inheritage + typedef over several layers

Discussion in 'C++' started by marcomoeller@googlemail.com, Jan 21, 2008.

  1. Guest

    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
     
    , Jan 21, 2008
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jan 21, 2008
    #2
    1. Advertising

  3. kwikius Guest

    On Jan 21, 8:39 pm, ""
    <> 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
     
    kwikius, Jan 21, 2008
    #3
    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. Zalek Bloom
    Replies:
    1
    Views:
    915
    Ryan Stewart
    Dec 26, 2003
  2. mandatory
    Replies:
    3
    Views:
    329
    mandatory
    Mar 1, 2005
  3. King Coffee

    Web.config file inheritage and cascading

    King Coffee, Mar 16, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    587
    Alexey Smirnov
    Mar 16, 2009
  4. Guest
    Replies:
    0
    Views:
    111
    Guest
    Apr 12, 2006
  5. Replies:
    4
    Views:
    79
Loading...

Share This Page