help in inheritance and typedef

Discussion in 'C++' started by Rex_chaos, Oct 9, 2003.

  1. Rex_chaos

    Rex_chaos Guest

    Hi all,
    I am going to do something just like the following tell

    template <class A>
    class Base
    {
    typedef Base& ref;

    public:
    void show(void)
    {
    cout << "Base" << endl;
    }

    ref get(void)
    {
    return *this;
    }
    };

    template <class A>
    class Derive: public Base<A>
    {
    typedef Derive& ref;

    public:
    void show(void)
    {
    cout << "Derive" << endl;
    }
    };

    Here, I have a "type" ref defined in the base class. It refers to the
    type of the current class. I redefine the type in it's derive class
    and expect the
    get(void) method will return a right instance. However, it won't.
    Actually, I want a function in each class to tell the type itself. How
    can I do that ?
     
    Rex_chaos, Oct 9, 2003
    #1
    1. Advertising

  2. Rex_chaos wrote:
    ....

    > Here, I have a "type" ref defined in the base class. It refers to the
    > type of the current class. I redefine the type in it's derive class
    > and expect the
    > get(void) method will return a right instance. However, it won't.
    > Actually, I want a function in each class to tell the type itself. How
    > can I do that ?


    Why ?
     
    Gianni Mariani, Oct 9, 2003
    #2
    1. Advertising

  3. > Here, I have a "type" ref defined in the base class. It refers to the
    > type of the current class. I redefine the type in it's derive class
    > and expect the
    > get(void) method will return a right instance. However, it won't.
    > Actually, I want a function in each class to tell the type itself. How
    > can I do that ?


    C++ has covariant return values :


    class A
    {
    public:
    virtual A get();
    };

    class B : public A
    {
    public:
    virtual B get();
    };


    Jonathan
     
    Jonathan Mcdougall, Oct 9, 2003
    #3
  4. > > C++ has covariant return values :
    >
    > It does.
    >
    > > class A
    > > {
    > > public:
    > > virtual A get();
    > > };
    > >
    > > class B : public A
    > > {
    > > public:
    > > virtual B get();
    > > };
    > >

    > These are not legal covariant types. It only works with pointers or

    references.
    > The above is ill-formed.


    class A
    {
    public:
    virtual A& get();
    };

    class B : public A
    {
    public:
    virtual B& get();
    };

    I think I'll take vacations for the next couple of days.

    Later,


    Jonathan
     
    Jonathan Mcdougall, Oct 9, 2003
    #4
  5. Rex_chaos

    Ron Natalie Guest

    "Jonathan Mcdougall" <> wrote in message news:RXghb.14408$...

    >
    > C++ has covariant return values :


    It does.

    > class A
    > {
    > public:
    > virtual A get();
    > };
    >
    > class B : public A
    > {
    > public:
    > virtual B get();
    > };
    >

    These are not legal covariant types. It only works with pointers or references.
    The above is ill-formed.
     
    Ron Natalie, Oct 9, 2003
    #5
  6. Rex_chaos

    klaas Guest

    Rex_chaos wrote:

    > Hi all,
    > I am going to do something just like the following tell
    >
    > template <class A>
    > class Base
    > {
    > typedef Base& ref;
    >
    > public:
    > void show(void)
    > {
    > cout << "Base" << endl;
    > }
    >
    > ref get(void)
    > {
    > return *this;
    > }
    > };
    >
    > template <class A>
    > class Derive: public Base<A>
    > {
    > typedef Derive& ref;
    >
    > public:
    > void show(void)
    > {
    > cout << "Derive" << endl;
    > }
    > };
    >
    > Here, I have a "type" ref defined in the base class. It refers to the
    > type of the current class. I redefine the type in it's derive class
    > and expect the
    > get(void) method will return a right instance. However, it won't.
    > Actually, I want a function in each class to tell the type itself. How
    > can I do that ?

    You are lucky that someone has thought about you because you can
    override returntypes in functions:
    so you define:
    template <class A>
    A& get()/*c++ do not use void here!!*/
    {/*whatever code you like*/
    };

    you just HAVE to redefine the get function to be able to do such thing
    I'm afraid...
     
    klaas, Oct 9, 2003
    #6
  7. Rex_chaos wrote in news::

    > Here, I have a "type" ref defined in the base class. It refers to the
    > type of the current class. I redefine the type in it's derive class
    > and expect the
    > get(void) method will return a right instance. However, it won't.
    > Actually, I want a function in each class to tell the type itself. How
    > can I do that ?
    >


    #include <iostream>
    #include <ostream>

    template <class A, class Ref = Base< A > >
    class Base
    {
    typedef Ref& ref;

    public:
    void show(void)
    {
    std::cout << "Base" << std::endl;
    }

    ref get(void)
    {
    return static_cast< ref >( *this );
    // cast is base to derived
    }
    };

    template <class A>
    class Derive: public Base<A, Derive< A > >
    {
    public:
    void show(void)
    {
    std::cout << "Derive" << std::endl;
    }
    };


    int main()
    {
    Derive< int > di;
    di.get().show();
    }


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Oct 9, 2003
    #7
    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. =?ISO-8859-1?Q?S=F8ren?= Holstebroe

    g++ typedef inheritance

    =?ISO-8859-1?Q?S=F8ren?= Holstebroe, Mar 6, 2005, in forum: C++
    Replies:
    2
    Views:
    2,119
    =?ISO-8859-1?Q?S=F8ren?= Holstebroe
    Mar 6, 2005
  2. , India
    Replies:
    5
    Views:
    521
    Kai-Uwe Bux
    Oct 25, 2007
  3. Carmen Sei

    typedef vs inheritance

    Carmen Sei, Apr 17, 2008, in forum: C++
    Replies:
    7
    Views:
    926
    James Kanze
    Apr 17, 2008
  4. oor
    Replies:
    0
    Views:
    1,370
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    613
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page