How to override virtual method of inner class?

Discussion in 'C++' started by Ralf, Mar 12, 2009.

  1. Ralf

    Ralf Guest

    Problem: Overriding virtual method of inner class of base, in a derived class.
    The following gives compilation error.
    How to solve this problem?

    class TCB
    {
    public:
    TCB() { }

    struct TS
    {
    void Print() { /*...*/ ; PrintMore(); }
    virtual void PrintMore() { }
    } S;
    };

    class TCD : virtual public TCB
    {
    public:
    TCD() { }
    void TS::printMore() { /*...*/ } // test.cpp:19: error: cannot define member function ‘TCB::TS::printMore’ within ‘TCD’
    };

    int main(int argc, char* argv[])
    {
    TCD O;
    O.S.Print();
    return 0;
    }
     
    Ralf, Mar 12, 2009
    #1
    1. Advertising

  2. Ralf

    Ralf Guest

    blargg wrote:
    > Ralf wrote:
    >
    >> Problem: Overriding virtual method of inner class of base, in a derived
    >> class.
    >> The following gives compilation error.
    >> How to solve this problem?
    >>
    >> class TCB
    >> {
    >> public:
    >> TCB() { }
    >>
    >> struct TS
    >> {
    >> void Print() { /*...*/ ; PrintMore(); }
    >> virtual void PrintMore() { }
    >> } S;
    >> };
    >>
    >> class TCD : virtual public TCB
    >> {
    >> public:
    >> TCD() { }
    >> void TS::printMore() { /*...*/ } // test.cpp:19: error: cannot define
    >> member function ŒTCB::TS::printMore¹ within ŒTCD¹
    >> };
    >>
    >> int main(int argc, char* argv[])
    >> {
    >> TCD O;
    >> O.S.Print();
    >> return 0;
    >> }

    >
    > An inner class is a class whose name is merely nested within the
    > enclosing class. Your example above is little different than (sorry, had
    > to change the names; much clearer this way)
    >
    > struct X { virtual void f(); };
    >
    > struct B { X x; };
    >
    > struct D : virtual B
    > {
    > // B has no virtual functions to override
    > };



    Sorry, I don't understand what you mean.
    The question is:
    How can I override the virtual method PrintMore() of the inner class?
    The derived class is just an helper, but it doesn't work.
    My intuition says that what I want should be possible somehow, but how?
     
    Ralf, Mar 12, 2009
    #2
    1. Advertising

  3. Ralf

    Steve Wolter Guest

    On 2009-03-12, Ralf <> wrote:
    > blargg wrote:
    >> Ralf wrote:
    >>
    >>> Problem: Overriding virtual method of inner class of base, in a derived
    >>> class.
    >>> The following gives compilation error.
    >>> How to solve this problem?
    >>>
    >>> class TCB
    >>> {
    >>> public:
    >>> TCB() { }
    >>>
    >>> struct TS
    >>> {
    >>> void Print() { /*...*/ ; PrintMore(); }
    >>> virtual void PrintMore() { }
    >>> } S;
    >>> };
    >>>
    >>> class TCD : virtual public TCB
    >>> {
    >>> public:
    >>> TCD() { }
    >>> void TS::printMore() { /*...*/ } // test.cpp:19: error: cannot define
    >>> member function ¼TCB::TS::printMore¹ within ¼TCD¹
    >>> };
    >>>
    >>> int main(int argc, char* argv[])
    >>> {
    >>> TCD O;
    >>> O.S.Print();
    >>> return 0;
    >>> }

    >>
    >> An inner class is a class whose name is merely nested within the
    >> enclosing class. Your example above is little different than (sorry, had
    >> to change the names; much clearer this way)
    >>
    >> struct X { virtual void f(); };
    >>
    >> struct B { X x; };
    >>
    >> struct D : virtual B
    >> {
    >> // B has no virtual functions to override
    >> };

    >
    >
    > Sorry, I don't understand what you mean.
    > The question is:
    > How can I override the virtual method PrintMore() of the inner class?
    > The derived class is just an helper, but it doesn't work.
    > My intuition says that what I want should be possible somehow, but how?


    It certainly is, and blargg had the right answer: The only connection
    an inner class has to the outer is the namespace, thus you must derive
    the overriding class from the inner class. So,

    class TCD : virtual public TCB::TS

    should do the trick.

    Regards, Steve

    --
    Steve Wolter (Bielefeld Univ.) | Web page: http://swolter.sdf1.org
    | vCard: http://swolter.sdf1.org/swolter.vcf
    A witty saying proves nothing. | Schedule: http://swolter.sdf1.org/sched.cgi
    -- Voltaire (1694-1778) | E-mail:
     
    Steve Wolter, Mar 12, 2009
    #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. E11
    Replies:
    1
    Views:
    4,786
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    18
    Views:
    9,332
    Roedy Green
    Jan 16, 2006
  3. jlopes
    Replies:
    7
    Views:
    427
    jlopes
    Nov 19, 2004
  4. Pyenos
    Replies:
    2
    Views:
    389
    Pyenos
    Dec 27, 2006
  5. Ethan
    Replies:
    4
    Views:
    740
Loading...

Share This Page