473,405 Members | 2,349 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,405 software developers and data experts.

baseclass enforcing one of two methods

Sorry the subject line isnt more explanatory, difficult to be succinct:

I have a base class to a bunch of classes which fill a data structure
and return that data structure, in each case the 'filling' method can
have Either the signature of

bool fillData();

OR

bool fillData(int &i, list<string& changeParams);

Is it possible to make a baseclass which will enforce that the derived
classes have one or other of these functions? so far my baseclass has

virtual bool fillData(){}
virtual bool fillData(int &i, list<string& changeParams){}

i.e. a default 'do nothing' implementation for both, which doesnt
enforce anything.

cheers
shaun
Jan 18 '07 #1
6 2599

shaun roe napsal:
Sorry the subject line isnt more explanatory, difficult to be succinct:

I have a base class to a bunch of classes which fill a data structure
and return that data structure, in each case the 'filling' method can
have Either the signature of

bool fillData();

OR

bool fillData(int &i, list<string& changeParams);

Is it possible to make a baseclass which will enforce that the derived
classes have one or other of these functions? so far my baseclass has

virtual bool fillData(){}
virtual bool fillData(int &i, list<string& changeParams){}

i.e. a default 'do nothing' implementation for both, which doesnt
enforce anything.

cheers
shaun
It is AFAIK not possible. You can use some workaround:
- write default implementation to throw and error (test for definition
will not be done during compilation, but you will be able to see it in
program)
- define both function as one function with default parameter values.
When you are using references, you can set default values to some
private static dummy variables and in the method you can easily check,
whether is it the default value. Then this method may be pure virtual
in base class
- You can define only one method again with one parameter which is
const reference to some class/struct instance which hold required
parameters

Jan 18 '07 #2
shaun roe wrote:
Sorry the subject line isnt more explanatory, difficult to be succinct:

I have a base class to a bunch of classes which fill a data structure
and return that data structure, in each case the 'filling' method can
have Either the signature of

bool fillData();

OR

bool fillData(int &i, list<string& changeParams);

Is it possible to make a baseclass which will enforce that the derived
classes have one or other of these functions? so far my baseclass has

virtual bool fillData(){}
virtual bool fillData(int &i, list<string& changeParams){}

i.e. a default 'do nothing' implementation for both, which doesnt
enforce anything.
No, but you could make derviced classes support both by making them
pure virtual functions. See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-22.4

If you made the base class have a default implementation as well:

class Base
{
virtual bool f() = 0 { return true; }
virtual bool f( int ) = 0 { return true; }
};

Then the Derived classes must override both, but they could call the
base implementation explicitly if they had nothing to add:

class Derived : public Base
{
virtual bool f() { /*Do something Derived specific */ return true; }
virtual bool f( int i ) { return Base::f( i ); }
};

BTW, if your derived classes don't support the full interface provided
by the base, then you don't have true polymorphism, which may indicate
a design flaw.

Cheers! --M

Jan 18 '07 #3
In article <11**********************@l53g2000cwa.googlegroups .com>,
"mlimber" <ml*****@gmail.comwrote:
shaun roe wrote:
Sorry the subject line isnt more explanatory, difficult to be succinct:

I have a base class to a bunch of classes which fill a data structure
and return that data structure, in each case the 'filling' method can
have Either the signature of

bool fillData();

OR

bool fillData(int &i, list<string& changeParams);

Is it possible to make a baseclass which will enforce that the derived
classes have one or other of these functions? so far my baseclass has

virtual bool fillData(){}
virtual bool fillData(int &i, list<string& changeParams){}

i.e. a default 'do nothing' implementation for both, which doesnt
enforce anything.

No, but you could make derviced classes support both by making them
pure virtual functions. See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-22.4

If you made the base class have a default implementation as well:

class Base
{
virtual bool f() = 0 { return true; }
virtual bool f( int ) = 0 { return true; }
};

Then the Derived classes must override both, but they could call the
base implementation explicitly if they had nothing to add:

class Derived : public Base
{
virtual bool f() { /*Do something Derived specific */ return true; }
virtual bool f( int i ) { return Base::f( i ); }
};

BTW, if your derived classes don't support the full interface provided
by the base, then you don't have true polymorphism, which may indicate
a design flaw.

Cheers! --M
Thanks, this looks ok; although I agree I might discover that your last
comment is the most relevant...
Jan 18 '07 #4
mlimber wrote:
class Base
{
need "public:" here
virtual bool f() = 0 { return true; }
"error: pure-specifier on function-definition"
In new versions of C++ you need define separate protected member:

protected:
inline bool implementation_f() { return true; }

and use "implementation_f" in derived if needed.
virtual bool f( int ) = 0 { return true; }
};
class Derived : public Base
{
virtual bool f() { /*Do something Derived specific */ return true; }
virtual bool f( int i ) { return Base::f( i ); }
And instead of explicit forwarding here, it is possible to use "using"
keyword, like this

class Derived : public Base
{
using Base::f;
bool f() { /*Do something Derived specific */ return true; }
};

Jan 19 '07 #5
In article <11*********************@m58g2000cwm.googlegroups. com>,
"Grizlyk" <gr******@yandex.ruwrote:
mlimber wrote:
class Base
{
need "public:" here
virtual bool f() = 0 { return true; }
"error: pure-specifier on function-definition"
In new versions of C++ you need define separate protected member:

protected:
inline bool implementation_f() { return true; }

and use "implementation_f" in derived if needed.
virtual bool f( int ) = 0 { return true; }
};
class Derived : public Base
{
virtual bool f() { /*Do something Derived specific */ return true; }
virtual bool f( int i ) { return Base::f( i ); }

And instead of explicit forwarding here, it is possible to use "using"
keyword, like this

class Derived : public Base
{
using Base::f;
bool f() { /*Do something Derived specific */ return true; }
};
Thanks; I quickly saw that my compiler (gcc 3.23) didn't like

virtual bool f() = 0 {return true;}

complaining that I was treating f() like a variable. Hopefully tour
approach will knock it on the head...

cheers
shaun
Jan 19 '07 #6
Grizlyk wrote:
mlimber wrote:
class Base
{
need "public:" here
Or "protected:". Mea culpa.
virtual bool f() = 0 { return true; }
"error: pure-specifier on function-definition"
In new versions of C++ you need define separate protected member:

protected:
inline bool implementation_f() { return true; }

and use "implementation_f" in derived if needed.
Mea culpa again, but you don't need a separate function; you just can't
define the function at the point of definition on compliant compilers
(several, including a couple of mine, allow it as an extension,
however). This should work everywhere:

class Base
{
protected:
virtual bool f() = 0;
virtual bool f( int ) = 0;
};

bool Base::f() { return true; }
bool Base::f(int) { return false; }
virtual bool f( int ) = 0 { return true; }
};
class Derived : public Base
{
virtual bool f() { /*Do something Derived specific */ return true; }
virtual bool f( int i ) { return Base::f( i ); }

And instead of explicit forwarding here, it is possible to use "using"
keyword, like this

class Derived : public Base
{
using Base::f;
bool f() { /*Do something Derived specific */ return true; }
};
No you can't because the function is a pure virtual and must be
explicitly overridden in the derived class.

Cheers! --M

Jan 19 '07 #7

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

Similar topics

8
by: DKode | last post by:
Ok, here is another question I have that is sort of unrelated to my last posting about composition. I have three Collection classes: EmployeeCollection HourCollection InfractionCollection
7
by: verbatime | last post by:
Please explain me how this works - or should work: Got my two classes - bcBasic (baseclass) and the derived cBasic. //--------------------------------------- class bcBasic { int number;...
4
by: Kimmo Laine | last post by:
Hi, is there a way to hide baseclass members: class BaseClass { public BaseClass() {} public string Get() { return "base"; } } class MyClass: BaseClass {
10
by: Michael Maes | last post by:
Hi, I have a BaseClass from which many Classes Derive. In short: the BaseClass provides the functionalities (Methods) and the Derived Classes extend it with Properties. One of the (Base)...
3
by: Michael Maes | last post by:
Hi, I have a BaseClass-DerivedClass Instance-Issue. Classes: (Framework) Stegosoft.Settings.SerializationClass Stegosoft.Stegosuite.Common.UIStrings (Inherits...
10
by: ddtbhai | last post by:
Hello folks, Just wanted to know if there are some 'standard' approaches to enforcing an order in the invocation of calling functions. It is usually needed when initializing some object. e.g...
20
by: Chris | last post by:
I'm not sure if this has been done before, but I couldn't easily find any prior work on Google, so here I present a simple decorator for documenting and verifying the type of function arguments....
7
by: montyphyton | last post by:
Some recent posts about Python programming style got me thinking. Since we have the PEP 8 which gives some guidelines about the style to be used, do we have any program that can check for...
8
by: carton | last post by:
Any opinions on this code as part of a general purpose framework? I expect people will have varying opinions on the use of $ in the property names. I did it for several reasons: 'interface' is...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.