| Author |
Message |
Mirko Puhic *nix forums beginner
Joined: 21 Jul 2006
Posts: 5
|
Posted: Fri Jul 21, 2006 1:53 am 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{
}; |
|
| Back to top |
|
 |
Alan Johnson *nix forums Guru Wannabe
Joined: 14 May 2005
Posts: 206
|
Posted: Fri Jul 21, 2006 2:38 am 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{
};
|
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
|
Posted: Fri Jul 21, 2006 2:43 am Post subject:
Re: class that have a member of type that's derived from it
|
|
|
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
|
Posted: Fri Jul 21, 2006 2:55 am Post subject:
Re: class that have a member of type that's derived from it
|
|
|
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
|
Posted: Fri Jul 21, 2006 3:42 am Post subject:
Re: class that have a member of type that's derived from it
|
|
|
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
|
Posted: Fri Jul 21, 2006 11:23 am Post subject:
Re: class that have a member of type that's derived from it
|
|
|
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
|
Posted: Fri Jul 21, 2006 11:53 am Post subject:
Re: class that have a member of type that's derived from it
|
|
|
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
|
Posted: Fri Jul 21, 2006 1:18 pm Post subject:
Re: class that have a member of type that's derived from it
|
|
|
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
|
Posted: Fri Jul 21, 2006 1:22 pm Post subject:
Re: class that have a member of type that's derived from it
|
|
|
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 |
|
 |
|