473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Question about inherited class virtual functions

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
Aug 8 '06 #1
3 1279
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.
Aug 8 '06 #2

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

Aug 8 '06 #3
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
11**********************@h48g2000cwc.googlegroups. com, "Alan Johnson"
<aw***@yahoo.comwrote:
>
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).
Aug 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
16
by: BCC | last post by:
Hi, If I have a base class with a virtual function defined: class CBase { virtual void Foo(); }; void CBase::Foo() { // base class functionality
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
3
by: marv | last post by:
If I have abstract base classes like these: //--------- class IBase { public: virtual void Action(void) = 0; };
5
by: gouqizi.lvcha | last post by:
Hi, all: I have 3 class X, Y, Z class Y is a subclass of class X; class Z is a subclass of class Y; i.e. class Y : public class X class Z : public class Y
26
by: Michael A. Covington | last post by:
Suppose class X has some public properties, and class Y inherits from X. Is there a way that some of the public properties of X can be private (hidden) in Y (but still usable by the methods...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
12
by: Mike - EMAIL IGNORED | last post by:
Within class MyClass, I can think of two ways to tell if MyClass is inherited in a particular use: 1. pass an appropriate bool in the ctor args; 2. use a virtual method that returns, for...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.