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

pointer to a derived class type

P: n/a
Hello, I've a small problem with something like this:

I've class A and class B, where B is derived from A. In the A class I
need to have a pointer to type of class B. I need to have a pointer to
a array of objects of class B in my A class. Anyone can help?

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
michi wrote:
Hello, I've a small problem with something like this:

I've class A and class B, where B is derived from A. In the A class I
need to have a pointer to type of class B. I need to have a pointer to
a array of objects of class B in my A class. Anyone can help?


// forward declarations
class B;
class A {
class B* b;
public:
};
Jul 23 '05 #2

P: n/a
michi wrote:
Hello, I've a small problem with something like this:

I've class A and class B, where B is derived from A. In the A class I
need to have a pointer to type of class B. I need to have a pointer to
a array of objects of class B in my A class. Anyone can help?


class B;
class A {
B* pb;
// blah
};

class B: public A {
// bleh
};

V
Jul 23 '05 #3

P: n/a
thanks lilburne, thanks Victor, you helped me a lot.

Jul 23 '05 #4

P: n/a
michi wrote:
Hello, I've a small problem with something like this:

I've class A and class B, where B is derived from A. In the A class I
need to have a pointer to type of class B. I need to have a pointer to
a array of objects of class B in my A class. Anyone can help?


Whilst the others have shown you how to do it...it is worth pointing out
that a base class knowing about its derived types is not a good design.

Could the A class not contain a pointer to an array of 'A's, that way,
should a new derived class 'C' come along, the array would be able to
store both B and C objects.

Andrew
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.