neted class & virtual function

Discussion in 'C++' started by fintrack@gmail.com, Sep 13, 2006.

  1. Guest

    HI,

    how do i overide a virtual function in a innerclass. the scenario is
    like

    class A
    {
    public:
    class B
    {
    public:
    virtual void go(void);
    void f(void);
    };
    }

    class A::B::go(void)
    {
    f();
    }

    I want to write a class C derived from class A and override the
    function go() is it possible?

    TIA,
    fin.
    , Sep 13, 2006
    #1
    1. Advertising

  2. wrote:
    > HI,
    >
    > how do i overide a virtual function in a innerclass. the scenario is
    > like
    >
    > class A
    > {
    > public:
    > class B
    > {
    > public:
    > virtual void go(void);
    > void f(void);
    > };
    > }
    >
    > class A::B::go(void)

    Error.
    void A::B::go(void)
    > {
    > f();
    > }
    >
    > I want to write a class C derived from class A and override the
    > function go() is it possible?


    I think, it's possible.

    > TIA,
    > fin.
    >
    Carlos Martinez, Sep 13, 2006
    #2
    1. Advertising

  3. Bart Guest

    wrote:
    > how do i overide a virtual function in a innerclass. the scenario is
    > like
    >
    > class A
    > {
    > public:
    > class B
    > {
    > public:
    > virtual void go(void);
    > void f(void);
    > };
    > }
    >
    > class A::B::go(void)
    > {
    > f();
    > }
    >
    > I want to write a class C derived from class A and override the
    > function go() is it possible?


    Yes, but you have to explicitly derive C::B from A::B like so:

    class C : public A
    {
    public:
    class B : public A::B
    {
    public: virtual void go(void);
    };
    };

    If you tell us what you're trying to do maybe we can suggest something
    else, like a template for example.

    Regards,
    Bart.
    Bart, Sep 13, 2006
    #3
  4. Guest

    Hi Bart,

    I have a library which has class A in it as above.

    I want to change the method go() in it to something else than done in
    base class.

    i cant change the base class.

    if i derive it the way you said how do i call it in my main program??


    thanks,
    fin
    , Sep 13, 2006
    #4
  5. Bart Guest

    wrote:
    > I have a library which has class A in it as above.
    >
    > I want to change the method go() in it to something else than done in
    > base class.
    >
    > i cant change the base class.
    >
    > if i derive it the way you said how do i call it in my main program??


    As with any other class. You create an instance of the derived class
    whenever an instance of the base class could be used. If this class is
    called back by the A class then you probably need to pass an object as
    a parameter or something of that sort. I can't really say just by
    looking at your short example.

    Regards,
    Bart.
    Bart, Sep 13, 2006
    #5
    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. Xiangliang Meng
    Replies:
    2
    Views:
    404
    Jack Klein
    Jun 21, 2004
  2. IK
    Replies:
    2
    Views:
    590
    hemraj
    Jul 23, 2004
  3. Ashwin
    Replies:
    2
    Views:
    336
    Pierre Barbier de Reuille
    Aug 1, 2006
  4. Replies:
    11
    Views:
    680
    James Kanze
    Sep 10, 2006
  5. Replies:
    7
    Views:
    574
    James Kanze
    May 2, 2007
Loading...

Share This Page