Question about inherited class virtual functions

B

Bint

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
 
R

red floyd

Bint said:
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.
 
A

Alan Johnson

Bint said:
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).
 
B

Bint

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top