niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
class that have a member of type that's derived from it
Post new topic   Reply to topic Page 1 of 1 [9 Posts] View previous topic :: View next topic
Author Message
Mirko Puhic
*nix forums beginner


Joined: 21 Jul 2006
Posts: 5

PostPosted: Fri Jul 21, 2006 1:53 am    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{

};
Back to top
Alan Johnson
*nix forums Guru Wannabe


Joined: 14 May 2005
Posts: 206

PostPosted: Fri Jul 21, 2006 2:38 am    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{

};

This is called the Curiously Recurring Template Pattern (honestly).

template <typename DerivedType>
struct Base
{
DerivedType der ;
} ;

struct Derived : public Base<Derived>
{
} ;

--
Alan Johnson
Back to top
Alan Johnson
*nix forums Guru Wannabe


Joined: 14 May 2005
Posts: 206

PostPosted: Fri Jul 21, 2006 2:43 am    Post subject: Re: class that have a member of type that's derived from it Reply with quote

Alan Johnson wrote:
Quote:
Mirko Puhic wrote:
Is there a way to properly do this?



struct Derived;

struct Base{
Derived der;
};

struct Derived: public Base{

};

This is called the Curiously Recurring Template Pattern (honestly).

template <typename DerivedType
struct Base
{
DerivedType der ;
} ;

struct Derived : public Base<Derived
{
} ;


Except, I should have run it through a compiler before posting. That
won't work. And object of type Derived can't contain a member (directly
or via inheritance) of type Derived, because that member would, of
course, also contain an member of type Derived, which would also ... you
see the problem.

What you CAN do is have a pointer or reference. Example:
struct Derived ;

template <typename DerivedType>
struct Base
{
DerivedType * der ;
} ;

struct Derived : public Base<Derived>
{
} ;

--
Alan Johnson
Back to top
Mirko Puhic
*nix forums beginner


Joined: 21 Jul 2006
Posts: 5

PostPosted: Fri Jul 21, 2006 2:55 am    Post subject: Re: class that have a member of type that's derived from it Reply with quote

Alan Johnson wrote:
Quote:
Alan Johnson wrote:
Mirko Puhic wrote:
Is there a way to properly do this?



struct Derived;

struct Base{
Derived der;
};

struct Derived: public Base{

};

This is called the Curiously Recurring Template Pattern (honestly).

template <typename DerivedType
struct Base
{
DerivedType der ;
} ;

struct Derived : public Base<Derived
{
} ;


Except, I should have run it through a compiler before posting. That
won't work. And object of type Derived can't contain a member (directly
or via inheritance) of type Derived, because that member would, of
course, also contain an member of type Derived, which would also ... you
see the problem.

What you CAN do is have a pointer or reference. Example:
struct Derived ;

template <typename DerivedType
struct Base
{
DerivedType * der ;
} ;

struct Derived : public Base<Derived
{
} ;


I see. Pointer will do. But in that case I think there's no need to use
a template:


struct Derived;

struct Base{

Derived * der;

};

struct Derived: public Base{

};
Back to top
Alan Johnson
*nix forums Guru Wannabe


Joined: 14 May 2005
Posts: 206

PostPosted: Fri Jul 21, 2006 3:42 am    Post subject: Re: class that have a member of type that's derived from it Reply with quote

Mirko Puhic wrote:
Quote:
Alan Johnson wrote:
Alan Johnson wrote:
Mirko Puhic wrote:
Is there a way to properly do this?



struct Derived;

struct Base{
Derived der;
};

struct Derived: public Base{

};

This is called the Curiously Recurring Template Pattern (honestly).

template <typename DerivedType
struct Base
{
DerivedType der ;
} ;

struct Derived : public Base<Derived
{
} ;


Except, I should have run it through a compiler before posting. That
won't work. And object of type Derived can't contain a member
(directly or via inheritance) of type Derived, because that member
would, of course, also contain an member of type Derived, which would
also ... you see the problem.

What you CAN do is have a pointer or reference. Example:
struct Derived ;

template <typename DerivedType
struct Base
{
DerivedType * der ;
} ;

struct Derived : public Base<Derived
{
} ;


I see. Pointer will do. But in that case I think there's no need to use
a template:


struct Derived;

struct Base{

Derived * der;

};

struct Derived: public Base{

};

True, though there are still times when that template pattern is useful.
Consider if you wanted to make a utility from which people could
derive to make their class into a linked list node. It might be useful
to do something like:

template <typename T>
struct Linkable
{
T * next ;
} ;

class MyType : public Linkable<MyType>
{
// ...
} ;

Another case is when you need each derived type to have its own copy of
static members.

template <typename T>
struct Base
{
// Note that this class doesn't actually use its template parameter.
static SomeType value ;
} ;

// A::value and B::value are separate objects.
class A : public Base<A> {}
class B : public Base<B> {}


More exotic uses arise in various template metaprogramming techniques.

--
Alan Johnson
Back to top
Mirko Puhic
*nix forums beginner


Joined: 21 Jul 2006
Posts: 5

PostPosted: Fri Jul 21, 2006 11:23 am    Post subject: Re: class that have a member of type that's derived from it Reply with quote

Alan Johnson wrote:
Quote:
Mirko Puhic wrote:
Alan Johnson wrote:
Alan Johnson wrote:
Mirko Puhic wrote:
Is there a way to properly do this?



struct Derived;

struct Base{
Derived der;
};

struct Derived: public Base{

};

This is called the Curiously Recurring Template Pattern (honestly).

template <typename DerivedType
struct Base
{
DerivedType der ;
} ;

struct Derived : public Base<Derived
{
} ;


Except, I should have run it through a compiler before posting. That
won't work. And object of type Derived can't contain a member
(directly or via inheritance) of type Derived, because that member
would, of course, also contain an member of type Derived, which would
also ... you see the problem.

What you CAN do is have a pointer or reference. Example:
struct Derived ;

template <typename DerivedType
struct Base
{
DerivedType * der ;
} ;

struct Derived : public Base<Derived
{
} ;


I see. Pointer will do. But in that case I think there's no need to
use a template:


struct Derived;

struct Base{

Derived * der;
};

struct Derived: public Base{
};

True, though there are still times when that template pattern is useful.
Consider if you wanted to make a utility from which people could derive
to make their class into a linked list node. It might be useful to do
something like:

template <typename T
struct Linkable
{
T * next ;
} ;

class MyType : public Linkable<MyType
{
// ...
} ;

Another case is when you need each derived type to have its own copy of
static members.

template <typename T
struct Base
{
// Note that this class doesn't actually use its template parameter.
static SomeType value ;
} ;

// A::value and B::value are separate objects.
class A : public Base<A> {}
class B : public Base<B> {}


More exotic uses arise in various template metaprogramming techniques.


Thenks, having a separate static member for each derived class seems
really useful.
Only problem with using pointer is that you can't create actual object
in the constructor because it would send the program into infinite
recursion.
Back to top
werasmus
*nix forums addict


Joined: 13 Jun 2005
Posts: 99

PostPosted: Fri Jul 21, 2006 11:53 am    Post subject: Re: class that have a member of type that's derived from it Reply with quote

Mirko Puhic wrote:

Quote:
Thenks, having a separate static member for each derived class seems
really useful.
Only problem with using pointer is that you can't create actual object
in the constructor because it would send the program into infinite
recursion.

You don't even need the seperate member in the base using CRTP. You
already have the implicit this pointer that can be casted to the
derived.

template <class DerivedT>
struct Base
{
void foo()
{
dynamic_cast<Derived*>(this)->doFoo();
}
};

class Derived : public Base<Derived>
{
public:
void doFoo(){}
};

I still can't see how you can get recursion... elaborate please.

Regards,

W
Back to top
Mirko Puhic
*nix forums beginner


Joined: 21 Jul 2006
Posts: 5

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

werasm wrote:
Quote:

I still can't see how you can get recursion... elaborate please.


In case I want Derived object to be created in Base constructor:


class Derived;

class Base{
public:
Base(){
der = new Derived();
}
Derived * der;

};

class Derived: public Base{

}
Back to top
Mirko Puhic
*nix forums beginner


Joined: 21 Jul 2006
Posts: 5

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

Mirko Puhic wrote:
Quote:
werasm wrote:

I still can't see how you can get recursion... elaborate please.


In case I want Derived object to be created in Base constructor:


class Derived;

class Base{
public:
Base(){
der = new Derived();
}
Derived * der;

};

class Derived: public Base{

}

I mean it's the same "recursion" problem as with using the type Derived
as an actual member (not pointer) of the class Base.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [9 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 9:12 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Unable to grant replication slave/client to class c Michael M. MySQL 0 Thu Jul 20, 2006 10:59 pm
No new posts Question about using copy constructor of parent class? LJB C++ 4 Thu Jul 20, 2006 9:32 pm
No new posts const char as class member markww C++ 3 Thu Jul 20, 2006 7:55 pm
No new posts Registering a data class with a handler class Chris Jewell C++ 6 Thu Jul 20, 2006 10:06 am
No new posts how to implement a simple class forname? Manuel C++ 11 Wed Jul 19, 2006 5:29 pm

Free Advertising | Myspace Layouts | Loans | Plumbing | Flights to Bangkok
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1677s ][ Queries: 16 (0.0702s) ][ GZIP on - Debug on ]