 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mirko Puhic Guest
|
Posted: Fri Jul 21, 2006 7:45 pm Post subject: class that have a member of type that's derived from it. |
|
|
Is there a way to properly do this?
struct Derived;
struct Base{
Derived der;
};
struct Derived: public Base{
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sat Jul 22, 2006 11:23 pm Post subject: Re: class that have a member of type that's derived from it. |
|
|
Mirko Puhic wrote:
| Quote: | Is there a way to properly do this?
struct Derived;
struct Base{
Derived der;
};
struct Derived: public Base{
};
|
I'll answer with another question: what is the size of Base? It
contains a Derived, which contains a Base, which contains a
Derived...
--
James Kanze kanze.james (AT) neuf (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
charleyb123@gmail.com Guest
|
Posted: Sat Jul 22, 2006 11:25 pm Post subject: Re: class that have a member of type that's derived from it. |
|
|
| Quote: | Mirko Puhic wrote:
Is there a way to properly do this?
struct Derived;
struct Base{
Derived der;
};
struct Derived: public Base{
};
|
As others mentioned, watch for "recursion" on
containment (you can't have an object that
contains itself which contains itself which
contains itself, ...ad infinitum )
But, if you're talking about some "utility"
class of derived type (perhaps it has no state,
or perhaps a static will do), you can try
something like:
<code>
struct Derived;
struct Base {
public:
virtual const Derived& getDerived(void) const = 0;
void someFunc(void) {
// ...call "getDerived()" all you want in here.
}
};
struct Derived : public Base {
static const Derived MY_INSTANCE_;
public:
virtual const Derived& getDerived(void) const {
return MY_INSTANCE_;
}
};
</code>
:%s/struct/class/g
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|