• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
XDarkAngelX

Getting the template type from a pointer to it

6 posts in this topic

I have a class template which inherits from a base class and the template class have a member variable that can be of any type( just like in stl continers).

 

 

class declartion:

class fooBase
{
public:
    int x;
    int y;

};

template <class T>
class fooTemplate : public fooBase
{
public:
    T data;
};

now after I  instantize the template class I save a pointer to it in a vector that holds pointers of the base class type.

 

all works fine till I wanted to get the pointer to my template class back and found out that I can't cast it back unless when I call the function to get the pointer back I tell it to what type of the template to cast it.

int main ()
{
	std::vector<fooBase*> foos;

 	fooTemplate<int>* fooTemp = new fooTemplate<int>;
	foos.push_back(fooTemp);
	
	getTemplate<int>(foos[0]);//works fine
	getTemplate(foos[0]); //compile error for not telling it the template type

	delete fooTemp;

	return 0;
}

my question is if there is a way to save type of the template so I don't need to tell the function the type of the template ?

or some way to catch if I by mistake I cast it to the wrong template type ?

 

also could I be just thinking of this all thing in the wrong way ?

0

Share this post


Link to post
Share on other sites
Please post the complete actual code you're using that creates this problem. If it's too large, please try and make a minimal - but complete - program which demonstrates your issue.
0

Share this post


Link to post
Share on other sites

Make fooBase a virtual class (add an empty virtual destructor), then you can at least dynamic_cast<fooTemplate<type>*>() to make sure that an object has type fooTemplate<type>. The calling function will still need to know the template type, but at least this adds type safety.

 

There isn't really any way to do better than that in C++, because there is no reflection support. If you want to perform an action on an object like that, the best way (that doesn't use down casting) would be to have a pure virtual function in fooBase that is implemented for each fooTemplate<type>.

Edited by Aressera
0

Share this post


Link to post
Share on other sites

You can't get the type back statically. The vector does not know the subclass type. That's the point of this kind of design.

 

Usually when you have an inheritance relationship like that, you should avoid casting to the child class, and instead use virtual methods.

 

It is possible to go back to the child type statically, using RTTI, but that has a runtime cost. I doubt that's what you want.

 

What problem are you trying to solve with this?

0

Share this post


Link to post
Share on other sites

The problem I am trying to solve is that I building a GUI and I have  a listbox and it stores items in it , each item has a text that gets renders for it and a Data that it holds(which can be of any type).

In order to get the current items selected in it i need to call function that is in the listbox that returns them to me.

The listbox itself inherits from control which is the base class for all the other controls.

0

Share this post


Link to post
Share on other sites

In that case you have a fixed universe of data types. In stead of using a template that can take any type, you should probably create a variant class that only handles that fixed list of types.

 

You don't know the type in the list at compile time, so you can't use anything that would deduce the static type for you. If you use inheritance, you need to rely on virtual functions to do something depending on the type of the data. In rare cases, you might need dynamic_cast.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0