C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

class that have a member of type that's derived from it.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Mirko Puhic
Guest





PostPosted: Fri Jul 21, 2006 7:45 pm    Post subject: class that have a member of type that's derived from it. Reply with quote



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





PostPosted: Sat Jul 22, 2006 11:23 pm    Post subject: Re: class that have a member of type that's derived from it. Reply with quote



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





PostPosted: Sat Jul 22, 2006 11:25 pm    Post subject: Re: class that have a member of type that's derived from it. Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.