Sign in to follow this  
Grant1219

Template question

Recommended Posts

I have a class that is like a custom container for any type of data, and I have another class that has a vector of those custom container classes. The problem is, I need to have a AddData(...) function that takes a custom data type, but I don't know how to get the type of a variable. Another problem is that I might need different data types stored in each container in the vector, but when you declare the vector you have to do vector<CDataContainer<type>> dataObjects, which doesn't allow you to have multiple types of variables.

Share this post


Link to post
Share on other sites
Hmmm.
I will give you this link:
http://www.catb.org/%7Eesr/faqs/smart-questions.html <<can this link be made a sticky?
As for the second question, you can have this link:
http://www.boost.org/doc/html/any.html

Share this post


Link to post
Share on other sites
Usually wanting to put unrelated types into a container in C++ (I'm assuming C++) indicates a design flaw, although there are exceptions (e.g. writing a scripting language). What exactly are you wanting to do?

Σnigma

Share this post


Link to post
Share on other sites
Have you considered a base class for all the objects that will be displayable on the GUI?
That way you could have a vector of pointers to organise all the different objects.

Share this post


Link to post
Share on other sites
Well the base class of the gui is CGUI, which handles windows, and buttons that aren't owned by a window. Each window handles it's own objects (which right now is only buttons, text, pictures and input boxes). kiome, do you mean have a class that handles all the window objects instead of having the windows handle their own objects? Actually, that would make it a lot easier to transfer objects from window to window, I never thought of doing that.

Share this post


Link to post
Share on other sites
What I meant was you could have a base class that would hold all gui relevant information and then you would have different classes that derive from the base class, yet they don't need to have anything else in common.
Then to display your gui all you would need is a vector that holds base class pointers to the actual derived class objects.
That way you would not need templates at all.

I will try to make a simple example:

class GUIBase
{
public:
int pos_x, pos_y;
SomeBitmapBuffer myIcon;
};

class Button : public GUIBase
{
public:
Button(SomeBitmapBuffer iconX, int x, int y)
{
myIcon = iconX;
pos_x = x;
pos_y = y;
}
};

int main()
{
Button b1;
vector<GUIBase*> guiElements;

guiElements.push_back(&b1);

/* Now you can access
guiElements.at(0)->pos_x; (pos_y and myIcon) */


return 0;
}




So you could get all the relevant gui information of the elements through the guiElements vector.

That means you can add random game items to the gui list, but they need to derive from the gui base class.

[Edited by - kiome on October 6, 2006 9:50:10 PM]

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