Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inheritage + typedef over several layers

Reply
Thread Tools

inheritage + typedef over several layers

 
 
marcomoeller@googlemail.com
Guest
Posts: n/a
 
      01-21-2008
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-21-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      01-21-2008
On Jan 21, 8:39*pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

> 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




 
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
Web.config file inheritage and cascading King Coffee ASP .Net 1 03-16-2009 08:45 AM
Subnets between core and access/distribution layers & routing between layers J Cisco 0 12-13-2006 08:38 PM
Problem executing several layers deep from a service bayxarea-usenet@yahoo.com Perl Misc 4 03-25-2005 11:23 PM
inheritage: possible to call the original function ? mandatory C++ 3 03-01-2005 09:18 AM
Newbie: Inheritage, overloading and casting Zalek Bloom Java 1 12-26-2003 02:57 PM



Advertisments