inheritage + typedef over several layers

M

marcomoeller

Hi!

I have something similar to:

--------
template <typename T>
class Base{
public:
typedef T TestType;
};

template <typename TypeParam>
class Derived : public Base<TypeParam > {
public:
TestType member;
};
----------------

and get the following error:
/home/caller/testIt/src/testit.cpp:40: error: 'TestType' does not name
a type
/home/caller/testIt/src/testit.cpp:40: note: (perhaps 'typename
Base<TypeParam>::TestType' was intended)


ok.. it WILL work with that tip... but in reality the baseclass is a
real complex construct, and I DONT what to write it a second time...
A part of the baseclasses job is to provide this types... why isn't it
possible??

THX

Marco

Is there a nice way to handle it, without
 
V

Victor Bazarov

I have something similar to:

--------
template <typename T>
class Base{
public:
typedef T TestType;
};

template <typename TypeParam>
class Derived : public Base<TypeParam > {
public:
TestType member;
};
----------------

and get the following error:
/home/caller/testIt/src/testit.cpp:40: error: 'TestType' does not name
a type
/home/caller/testIt/src/testit.cpp:40: note: (perhaps 'typename
Base<TypeParam>::TestType' was intended)


ok.. it WILL work with that tip... but in reality the baseclass is a
real complex construct, and I DONT what to write it a second time...
A part of the baseclasses job is to provide this types... why isn't it
possible??

Because I can provide a specialisation of your 'Base' in which
'TestType' is not necessarily a typename.
THX

Marco

Is there a nice way to handle it, without

No, there isn't.

V
 
K

kwikius

ok.. it WILL work with that tip... but in reality the baseclass is a
real complex construct, and I DONT what to write it a second time...

It is possible to use a typedef in place of Base<T>. This may help,
though its difficult to say without seing the code. Anyway,In the
following example the helper is longer than using Base<T> direct, but
in some situations it can be used to shorten things. Essentially what
I am describing is metaprogramming, and in the following
"construct_Base<T>" is a metafunction. Its arguments are its template
parameters and its "return type" is its member named "type". You don't
need to use that name but it has been popularised by boost.org and
works well as a convention:
It is of course useful to come up with a significant name for the
metafunction too.

(Untested)

// The actual class
template <typename T>
struct Base{ typedef T TestType;};

// A metafunction that "returns" some type
template <typename T>
struct construct_Base{
typedef Base<T> type;
};

template <typename T>
struct Derived : construct_Base<T>::type{
/* ^^ n.b no typename */
typedef typename construct_Base<T>::type base_type;
typedef typename base_type::TestType TestType;
TestType member;
};

HTH

regards
Andy Little
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top