Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   CRTP-problem: How can the base class typedef a derived class' typedef? (http://www.velocityreviews.com/forums/t615851-crtp-problem-how-can-the-base-class-typedef-a-derived-class-typedef.html)

oor 05-20-2008 12:39 PM

CRTP-problem: How can the base class typedef a derived class' typedef?
 
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;
*/
}
// --------------------------------------------


All times are GMT. The time now is 12:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57