 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jim Guest
|
Posted: Thu Oct 23, 2003 3:54 pm Post subject: Virtual Function Overloading |
|
|
I often face a similar problem where I have an object that needs to
calculate a set of values but in different ways, requiring different
supporting classes, etc. But the results are used in the same way by
all other objects. So, many parts are common.
The Base Class and multiple derived Classes seems the elegant
solution. But, because I get "'class': cannot instantiate abstract
class due to the following members" error, it seems illegal to have
Overloaded Pure Virtual Functions where not all of the derived classes
have all of the Pure Virtual Functions.
So, should the following oversimplified example be legal? If not,
what is the correct way to represent this type of problem?
Thanks
Jim
Class CBase{
public:
virtual void Precalculate(int);
virtual void Precalculate(double, double, double);
protected:
double dResults[10000];
}
Class DerivedA : public CBase{
public:
virtual void Precalculate(int);
}
Class DerivedB : public CBase{
public:
virtual void Precalculate(double, double, double);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Richard Smith Guest
|
Posted: Thu Oct 23, 2003 10:44 pm Post subject: Re: Virtual Function Overloading |
|
|
Jim wrote:
| Quote: | I often face a similar problem where I have an object that needs to
calculate a set of values but in different ways, requiring different
supporting classes, etc. But the results are used in the same way by
all other objects. So, many parts are common.
The Base Class and multiple derived Classes seems the elegant
solution. But, because I get "'class': cannot instantiate abstract
class due to the following members" error, it seems illegal to have
Overloaded Pure Virtual Functions where not all of the derived classes
have all of the Pure Virtual Functions.
|
This is correct. All pure virtual functions must be
overridden by the derived class if you want to be able to
create an object of that type. The fact that your functions
are overloaded is irrelevant -- each overload counts as an
entirely separate function and must be overridden.
| Quote: | Class CBase{
public:
virtual void Precalculate(int);
virtual void Precalculate(double, double, double);
|
These functions are not pure virtual; to be pure virutal you
need to put '= 0' before the semicolon.
| Quote: | Class DerivedA : public CBase{
public:
virtual void Precalculate(int);
}
Class DerivedB : public CBase{
public:
virtual void Precalculate(double, double, double);
}
|
As it stands, this code is legal, but assuming you meant to
make the functions in CBase pure virtual, then it would not
be legal.
Suppose I have a CBase (e.g. held by pointer), how do I know
what functions I can call on it? And what happens if I call
a pure virtual function that has not been overridden?
If you want to use inheritance and virtual functions like
this, then each class needs to have the *same* interface.
--
Richard Smith
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
stelios xanthakis Guest
|
Posted: Fri Oct 24, 2003 7:26 pm Post subject: Re: Virtual Function Overloading |
|
|
[email]jfinn (AT) rabar (DOT) com[/email] (Jim) wrote in message news:<b0bd38c4.0310221741.38542f1f (AT) posting (DOT) google.com>...
| Quote: | I often face a similar problem where I have an object that needs to
calculate a set of values but in different ways, requiring different
supporting classes, etc. But the results are used in the same way by
all other objects. So, many parts are common.
|
``Only common parts go in the base class''
| Quote: | The Base Class and multiple derived Classes seems the elegant
solution. But, because I get "'class': cannot instantiate abstract
class due to the following members" error, it seems illegal to have
Overloaded Pure Virtual Functions where not all of the derived classes
have all of the Pure Virtual Functions.
So, should the following oversimplified example be legal? If not,
what is the correct way to represent this type of problem?
Class CBase{
public:
virtual void Precalculate(int);
virtual void Precalculate(double, double, double);
protected:
double dResults[10000];
}
Class DerivedA : public CBase{
public:
virtual void Precalculate(int);
}
Class DerivedB : public CBase{
public:
virtual void Precalculate(double, double, double);
}
|
No. The functions Precalculate(int) and Precalculate(double,double,double)
have no reason to be in the base object. In the base object put only
what's common. For example:
class CBase {
double results [10000];
void present_results ();
void do_fast_fourier_transform_on_results ();
};
class DerrivedA : CBase {
void Precalculate (int);
};
class DerrivedB : CBase {
void Precalculate (double, double, double);
};
See if this works. If it does, then there's no need to
use virtual functions+virtual tables+overhead.
stelios
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|