Question about inherited class virtual functions

Discussion in 'C++' started by Bint, Aug 8, 2006.

  1. Bint

    Bint Guest

    Hello,

    I am wondering if someone can answer this for me. I have a base class,
    with function Copy(). It is supposed to make a copy of the class. I define
    it as virtual.

    Then I have a subclass of that, and I give it it's own Copy() function. How
    do I make sure that the subclass' Copy() function is called, and not the
    base class?


    For example:

    class Thread { public:
    unsigned char type;
    virtual Thread *Copy();
    };

    class geothread:public Thread { public:
    virtual Thread *Copy();
    };


    Thread *newt, *t = object->thread; // can be either base or subclass
    Newt = t->Copy();


    My program is always calling the base class' Copy(), when I want it to call
    whichever one is appropriate depending on whether object->thread is really a
    base or a sub. Is there anyway for it to know?

    Thanks
    B
    Bint, Aug 8, 2006
    #1
    1. Advertising

  2. Bint

    red floyd Guest

    Bint wrote:
    > Hello,
    >
    > I am wondering if someone can answer this for me. I have a base class,
    > with function Copy(). It is supposed to make a copy of the class. I define
    > it as virtual.
    >
    > Then I have a subclass of that, and I give it it's own Copy() function. How
    > do I make sure that the subclass' Copy() function is called, and not the
    > base class?
    >
    >
    > For example:
    >
    > class Thread { public:
    > unsigned char type;
    > virtual Thread *Copy();
    > };
    >
    > class geothread:public Thread { public:
    > virtual Thread *Copy();
    > };
    >
    >
    > Thread *newt, *t = object->thread; // can be either base or subclass
    > Newt = t->Copy();
    >
    >
    > My program is always calling the base class' Copy(), when I want it to call
    > whichever one is appropriate depending on whether object->thread is really a
    > base or a sub. Is there anyway for it to know?
    >



    Without seeing *actual* code, my only guess is that either "object" is
    of type Thread, or you forgot to declare Thread::Copy() virtual in your
    actual code.

    Post a minimal compilable example which exhibits the troublesome
    behavior, and then we can examine the issue. Otherwise, the answer is
    that you have an error on line 42 of your code.
    red floyd, Aug 8, 2006
    #2
    1. Advertising

  3. Bint

    Alan Johnson Guest

    Bint wrote:
    > Hello,
    >
    > I am wondering if someone can answer this for me. I have a base class,
    > with function Copy(). It is supposed to make a copy of the class. I define
    > it as virtual.
    >
    > Then I have a subclass of that, and I give it it's own Copy() function. How
    > do I make sure that the subclass' Copy() function is called, and not the
    > base class?
    >
    >
    > For example:
    >
    > class Thread { public:
    > unsigned char type;
    > virtual Thread *Copy();
    > };
    >
    > class geothread:public Thread { public:
    > virtual Thread *Copy();
    > };
    >
    >
    > Thread *newt, *t = object->thread; // can be either base or subclass
    > Newt = t->Copy();
    >
    >
    > My program is always calling the base class' Copy(), when I want it to call
    > whichever one is appropriate depending on whether object->thread is really a
    > base or a sub. Is there anyway for it to know?
    >
    > Thanks
    > B


    The code you posted looks correct, which means the problem is probably
    in some code that you didn't post. Please post a minimal compilable
    example that demonstrates the problem you are having (that means,
    something that we could copy and paste into an editor and compile to
    see the problem you are having).

    --
    Alan Johnson
    Alan Johnson, Aug 9, 2006
    #3
  4. Bint

    Bint Guest

    Yes, you were right. My sub Copy had an argument, whereas the base did not.
    So it wasn't seeing them as the same functions.

    Thanks
    B


    On 8/8/06 6:00 PM, in article
    , "Alan Johnson"
    <> wrote:

    >
    > Bint wrote:
    >> Hello,
    >>
    >> I am wondering if someone can answer this for me. I have a base class,
    >> with function Copy(). It is supposed to make a copy of the class. I define
    >> it as virtual.
    >>
    >> Then I have a subclass of that, and I give it it's own Copy() function. How
    >> do I make sure that the subclass' Copy() function is called, and not the
    >> base class?
    >>
    >>
    >> For example:
    >>
    >> class Thread { public:
    >> unsigned char type;
    >> virtual Thread *Copy();
    >> };
    >>
    >> class geothread:public Thread { public:
    >> virtual Thread *Copy();
    >> };
    >>
    >>
    >> Thread *newt, *t = object->thread; // can be either base or subclass
    >> Newt = t->Copy();
    >>
    >>
    >> My program is always calling the base class' Copy(), when I want it to call
    >> whichever one is appropriate depending on whether object->thread is really a
    >> base or a sub. Is there anyway for it to know?
    >>
    >> Thanks
    >> B

    >
    > The code you posted looks correct, which means the problem is probably
    > in some code that you didn't post. Please post a minimal compilable
    > example that demonstrates the problem you are having (that means,
    > something that we could copy and paste into an editor and compile to
    > see the problem you are having).
    Bint, Aug 9, 2006
    #4
    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. Replies:
    7
    Views:
    368
    Richard
    Aug 28, 2006
  2. John Goche
    Replies:
    10
    Views:
    720
    Marcus Kwok
    Dec 8, 2006
  3. vabby
    Replies:
    7
    Views:
    357
    Chris Theis
    Dec 16, 2006
  4. Angus
    Replies:
    5
    Views:
    1,375
    Krice
    Apr 10, 2008
  5. 7stud --
    Replies:
    11
    Views:
    385
    7stud --
    Nov 9, 2007
Loading...

Share This Page