Is there a way to properly do this?
struct Derived;
struct Base{
Derived der;
};
struct Derived: public Base{
}; 9 1662
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>
{
} ;
--
Alan Johnson
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>
{
} ;
--
Alan Johnson
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{
};
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.
--
Alan Johnson
Alan Johnson wrote:
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.
Mirko Puhic wrote:
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<De rived*>(this)->doFoo();
}
};
class Derived : public Base<Derived>
{
public:
void doFoo(){}
};
I still can't see how you can get recursion... elaborate please.
Regards,
W
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{
}
Mirko Puhic wrote:
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.
Mirko Puhic wrote:
In case I want Derived object to be created in Base constructor:
Why would you want do to that? Base can only be constructed upon
instantiating derived anyway....
Derived d;//...
constructs base implicitly, and the pointer in base becomes a valid
derived...
template <class DerivedT>
struct Base
{
Derived* getDerived(){ return dynamic_cast<De rivedT*>(this); }
};
class Derived : public Base<Derived>{ /*...*/ };
Derived::Derive d
: Base<Derived>()
{
//From this point onwards, we can no obtain valid derived pointer...
}
int main()
{
Derived d;
Base<Derived>* pb( &d );
pb->getDerived()->foo();//would work assuming Derived had member
function foo...
return 0;
}
class Derived;
class Base{
public:
Base(){
der = new Derived();
}
Derived * der;
};
You don't need to assign a new Derived to der, you don't even need a
der as this is implicitly already a pointer to the derived class if you
cast it, provided Derived is truly inherited from Base (which one can
enforce, btw).
Kind regards,
Werner This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Young |
last post by:
I've been trying to compile code similar to the example code below,
but I keep getting errors indicating that the function 'foo()' is not
accessible. At first, I thought this was a bug in the compiler, but
I've now tried the code against Borland C++ Builder 6.0, MS VC++ .NET,
DevC++ (GCC), and Comeau's on-line compiler, and all give me similar
diagnositic messages.
I can make the code work if I make the "protected" items "public" in...
|
by: kuvpatel |
last post by:
Hi
I want to refer a class called LogEvent, and use one of its methods called
WriteMessage without actually having to create an instance of Logevent.
I have tried using the word sealed with the class and this works but I would
also like to know of other ways to do this.
Also are there any performance implacations of using sealed?
|
by: Andy |
last post by:
Hi all,
I have a site with the following architecture:
Common.Web.dll - Contains a CommonPageBase class which inherits
System.Web.UI.Page
myadd.dll - Contains PageBase which inherits CommonPageBase
- Contains myPage which inherits PageBase
Each of these classes overrides OnInit and ties an event handler
|
by: olanglois |
last post by:
Hi,
I am not sure if I have found a compiler bug (I am using VC++.NET2003)
or if this is the correct behavior defined by the language but I am
sure someone can clear up my confusion. Suppose the following:
class Base
{
protected:
int x;
|
by: Dave Booker |
last post by:
It looks like the language is trying to prevent me from doing this sort of
thing. Nevertheless, the following compiles, and I'd like to know why it
doesn't work the way it should:
public class ComponentA
{
static string s_name = "I am the root class.";
public string Name { get {return s_name;} }
}
| |
by: Bob Johnson |
last post by:
I have a base class that must have a member variable populated by, and only
by, derived classes.
It appears that if I declare the variable as "internal protected" then the
base class *can* populate the variable, but the population is not *required*
by the derived class (which must be the case).
What would meet the requirements is if I create an abstract method in the
base class that populates the member variable. In this case the...
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I met with a strange issue that derived class function can not access base
class's protected member. Do you know why?
Here is the error message and code.
error C2248: 'base::~base' : cannot access protected member declared in
|
by: Immortal Nephi |
last post by:
The rule of inheritance states that you define from top to bottom.
Sometimes, you want to define base class and set reference from
dervied class to base class, but you violate the rule.
Here is an example of my code below.
class A {};
class B : public A {};
int main(void)
{
|
by: Juha Nieminen |
last post by:
Assume I have a class Base, and then this:
struct Derived: public Base
{
AnotherClass member;
};
Also assume that I'm allocating an object of type 'Derived' with a
custom allocator and that, for whatever reason, I don't want to call the
constructor of 'Derived', but instead I want to build the object by
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |