Class Instance as Member Variable

May 14, 2009 at 7:57pm
Hello,

My question is: Is there some way that I can have a class instance as a member variable of a class? For example, a very simple class:

class Tria {
private:

public:
//member variables
std::vector<Tria *> Neighbor_Map;

//methods
Tria() {;};
~Tria() {;};
};

This functionality would allow me not to have to create an external map that contained the connectivity between the Tria class. I could be in any Tria and get to a neighboring Tria. Even if this syntax is not possible in some way, is the functionality that I'm looking for possible?

Thanks for the Help,

DM
Last edited on May 14, 2009 at 7:57pm
May 14, 2009 at 8:02pm
Yes.
May 14, 2009 at 8:10pm
Thanks for the quick response.

That's wonderful news! I realize that you answered my question. :) But, could you please elaborate on how to do this? That would help tremendously. Again, thanks for the quick response and lesson learned on getting what you ask for.

DM
May 14, 2009 at 8:15pm
What you're doing there is exactly how it's done. As long as the class doesn't contain itself it's fine.
For example:
1
2
3
4
5
6
7
8
9
class A{
    A a; //Wrong!
    A *b //Right!
    B c //Wrong!
};

class B{
    A a;
};
Topic archived. No new replies allowed.