Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > CRTP-problem: How can the base class typedef a derived class' typedef?

Reply
Thread Tools

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

 
 
oor oor is offline
Junior Member
Join Date: May 2008
Posts: 1
 
      05-20-2008
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;
*/
}
// --------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
why can't derived class pointer can't point to base class object ypjofficial@indiatimes.com C++ 1 12-24-2004 08:46 AM
why can't derived class pointer can't point to base class object ypjofficial@indiatimes.com C++ 0 12-24-2004 06:34 AM



Advertisments