CRTP-problem: How can the base class typedef a derived class' typedef?

Discussion in 'C Programming' started by oor, May 20, 2008.

  1. oor

    oor

    Joined:
    May 20, 2008
    Messages:
    1
    CRTP: Curiously recurring template pattern

    Motivation:
    Instead of every derived class having to implement the getSomeClass() method, I want by static polymorphism the base class to do it more or less in a way outlined in the code commented out in the base class.

    In the example below, the derived class is a template class, I guess that's not important for the solution, but anyways, that's how I need it to be in my case.

    Anybody having an idea of how to achieve my goal?

    base_and_derived.h
    ===============
    // --------------------------------------------
    template <int J>
    class SomeClasss{
    };
    // --------------------------------------------
    template <template <int> class DERIVED>
    class Base {
    public:
    /*
    // I want, but cannot get it to work:
    typedef DERIVED::my_type same_type_higher_up;
    */
    /*
    // I want, but cannot get it to work:
    same_type_higher_up *getInstanceOfSameTypeHigherUp() const {
    return new same_type_higher_up();
    }
    */
    };
    // --------------------------------------------
    template <int I>
    class Derived : public Base<Derived> {

    public:
    typedef SomeClasss<I> my_type;

    public:
    my_type *getSomeClass() const{
    return new my_type();
    }

    };
    // --------------------------------------------
    main.cpp
    =======

    #include "base_and_derived.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
    // Works fine:
    Derived<7> *derived = new Derived<7>();
    Derived<7>::my_type *someClassInstance = derived->getSomeClass();
    delete someClassInstance;
    delete derived;

    /*
    // I want, but cannot get it to work:
    Derived<7> *derived = new Derived<7>();
    Derived<7>::same_type_higher_up *instanceOfSameTypeHigherUp = derived->getInstanceOfSameTypeHigherUp();
    delete instanceOfSameTypeHigherUp;
    delete derived;
    */
    }
    // --------------------------------------------
     
    oor, May 20, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ypjofficial@indiatimes.com
    Replies:
    1
    Views:
    1,324
    Jonathan Mcdougall
    Dec 24, 2004
  2. developereo@hotmail.com
    Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  3. developereo@hotmail.com
    Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  4. Frank Bergemann
    Replies:
    3
    Views:
    642
    peter koch
    May 2, 2008
  5. er ci
    Replies:
    1
    Views:
    285
Loading...

Share This Page