By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,516 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,516 IT Pros & Developers. It's quick & easy.

Help with finding a container to be used in a class.

P: 7

I wish to have a baseclass A that class B and C inherits from, C is a standard class that has some get and set functions while B is like C only with a container class that can have 1 or more references to classes of type B or C. Its kinda like building a treevies with classes. And I want to use a list of A classes so I can get the values for iteration through all the classes afterwards and obtaining the values they have.

Here is the baseclass:

Expand|Select|Wrap|Line Numbers
  1. class A {
  2.  private:
  3.   int m_weight;
  4.  public:
  5.   A(int weight) { m_weight = weight };
  6.   virtual int getValue();
  7.   virtual int getBits();
  8. }

here is the B class with some kind of container:

Expand|Select|Wrap|Line Numbers
  1. class B : public A {
  2.  private:
  3.   int m_value;
  4.   const static int m_bits = 8;
  5.  public:
  6.   B(int weight) : A(weight);
  7.   container?? Childs;
  8.   int getValue() { return m_value; };
  9.   int getBits() { return m_bits; };
  10. }

and here is the C class without the container

Expand|Select|Wrap|Line Numbers
  1. class C : public A {
  2.  private:
  3.   int m_value;
  4.   const static int m_bits = 8;
  5.  public:
  6.   B(int weight) : A(weight);
  7.   int getValue() { return m_value; };
  8.   int getBits() { return m_bits; };
  9. }

Weight is the factor on how to sort each class, and I will add them to a list of class A* that I will iterate through after a sorting them to get ahold of the value and bits of each class. I have looked some on map but I didn't find a good way to find out what type of class it is. The list might look something like this:

C has the class 1, 9.
B has the class 2, 4.
2 contains the C class 3
4 contais the C class 5, 6 and B class 7.
7 contains the C class 8.

How I want to access the value after iteration if the weight is the same as the name would look like:

Expand|Select|Wrap|Line Numbers
  1. 1->getValue()
  2. 2->getValue()
  3. 2->Childs(3)->getValue()
  4. 4->getValue()
  5. 4->Childs(5)->getValue()
  6. 4->Childs(6)->getValue()
  7. 4->Childs(7)->getValue()
  8. 4->Childs(7)->Childs(8)->getValue()

Now this might not be the best explanation and the access to subobjects is probably not done as I described but that is what I'm looking for and I need to get a pointer where to look (examples would be lovely) or if I should just drop it and think of it no more =).

// Andreas
Aug 9 '07 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
From what I see you might be interested in the Visitor design pattern. This is an article in the C/C++ Articles forum.

More information would be in the book Design Patrerns by Erich Gamma et al Addison-Wesley 1994 on page 331.
Aug 9 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.