Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

quirky inheritance in c++

Name: Anonymous 2007-02-25 2:58 ID:ZVnyBiRu

Wrap your head around this one:


template <typename T> class B { /*...*/ }

class A : public B<A> { /*...*/ }


Can anyone explain to me how the compiler manages to get this right?  (and it does, somehow)  Keep in mind the following:  Save for access restrictions, the class B<A> knows about any and all of A's constructors and static member functions.  At the same time, class A knows about any and all members of B<A>.

Doesn't the compiler have to compile the base class before compiling the derived?  That means compiling B<A> before A, but with the way templates work you have to compile A before B<A>.

Name: Anonymous 2007-02-25 3:30 ID:cfYOkQdV

try this:
template <class T>
class B {
private:
    T mT;
};

class A: public B<A> {};

Name: Anonymous 2007-02-25 8:30 ID:Wfhab/2d

>>1

You're referring to Coplien's curiosly recurring template pattern.  (http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern)

It's important to note that the base class is not intended to be used independently.  Because of this (as you can see in the linked wiki), you actually refer to the members of A by statically casting "this" to an A*, not by including an instance of A as a field.

Name: Anonymous 2007-02-25 16:45 ID:GgjQNlng

Use a language that does not require templates.

Name: Sgt.Kabuﱄ戫kiman珬ᗑ 2012-05-28 22:56

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List