473,406 Members | 2,843 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.

virtual functions/generic access


I am implementing some functionality where there is a single
base class 'A' which provides some common functionality

There are then two classes derived from 'A', call them 'B' and 'C'.
Both 'B' and 'C' define functions specific only to their class. Somethng
like...
class A
{
void doX();
void doY();
}

class B : class A
{
void getInfo();
char * getBInfo();
}

class C : class A
{
void getInfo();
char * getCInfo();
}
I then have a list of objects. Some are based on 'B', some are based
on 'C', but I would like to treat them generically.

ob[0]->type.doX(); // always ok
ob[0]->type.getInfo(); // always ok
ob[0]->type.getCInfo(); // only ok if type is Class C

I know if I make getBInfo() virtual in the base class then I can
make the above call regardless. But then I have to provide some
return value for it in class A.

If I make it a pure virtual, then I need to provide an implementation
for it in the derived class.

Is there a way to treat a list of objects like this generically without
having to know their
actual type?

It seems kludgey to have something like:

virtual char * getBInfo() {return NULL};

in my base class, but perhaps that's the way it's done.

Thanks
Mark


Jul 23 '07 #1
3 1497
Mark Foley wrote:
I am implementing some functionality where there is a single
base class 'A' which provides some common functionality

There are then two classes derived from 'A', call them 'B' and 'C'.
Both 'B' and 'C' define functions specific only to their class.
Somethng like...
class A
{
Probably

public:
void doX();
void doY();
}
;
>
class B : class A
You mean

class B : public A
{
Most likely

public:
void getInfo();
char * getBInfo();
}
;
>
class C : class A
You mean

class C : public A
{
void getInfo();
char * getCInfo();
}
;
>
I then have a list of objects. Some are based on 'B', some are based
on 'C', but I would like to treat them generically.

ob[0]->type.doX(); // always ok
ob[0]->type.getInfo(); // always ok
ob[0]->type.getCInfo(); // only ok if type is Class C
Are you claiming that that's what happens or setting forth requirements?
>
I know if I make getBInfo() virtual in the base class then I can
make the above call regardless.
Regardless of what? Which of the above calls is 'getBInfo()'?
But then I have to provide some
return value for it in class A.
If you intend to make it virtual and have it in 'A', you need to give
it the _covariant_ return value type. It can't just be "some".
If I make it a pure virtual, then I need to provide an implementation
for it in the derived class.
Only if you intend to instantiate the derived class.
Is there a way to treat a list of objects like this generically
without having to know their
actual type?
The only way to do that is to give them polymorphic behaviour (through
virtual functions, as you already mentioned).
It seems kludgey to have something like:

virtual char * getBInfo() {return NULL};

in my base class, but perhaps that's the way it's done.
It's _a_ way to do it. Whether it's the best way, is debatable.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 23 '07 #2

"Mark Foley" wrote in message
>
I am implementing some functionality where there is a single
base class 'A' which provides some common functionality

There are then two classes derived from 'A', call them 'B' and 'C'.
Both 'B' and 'C' define functions specific only to their class. Somethng
like...
class A
{
void doX();
void doY();
}

class B : class A
{
void getInfo();
char * getBInfo();
}

class C : class A
{
void getInfo();
char * getCInfo();
}
I then have a list of objects. Some are based on 'B', some are based
on 'C', but I would like to treat them generically.

ob[0]->type.doX(); // always ok
ob[0]->type.getInfo(); // always ok
ob[0]->type.getCInfo(); // only ok if type is Class C

I know if I make getBInfo() virtual in the base class then I can
make the above call regardless. But then I have to provide some
return value for it in class A.

If I make it a pure virtual, then I need to provide an implementation
for it in the derived class.

Is there a way to treat a list of objects like this generically without
having to know their
actual type?

It seems kludgey to have something like:

virtual char * getBInfo() {return NULL};

in my base class, but perhaps that's the way it's done.

Thanks
Mark
Mark,
Yes when you override a virtual function it must be declared as virtual in
the base class.
You can read more about virtual functions in
The C++ Programming Language, 3rd. ed., par. 12.2.6
and also on:
http://www.parashift.com/c++-faq-lit...functions.html
Regards, Maarten.


Jul 23 '07 #3

"Mark Foley" <fo****@meftechno.comwrote in message
news:46***********************@roadrunner.com...
>
I am implementing some functionality where there is a single
base class 'A' which provides some common functionality

There are then two classes derived from 'A', call them 'B' and 'C'.
Both 'B' and 'C' define functions specific only to their class. Somethng
like...
class A
{
void doX();
void doY();
}

class B : class A
{
void getInfo();
char * getBInfo();
}

class C : class A
{
void getInfo();
char * getCInfo();
}
I then have a list of objects. Some are based on 'B', some are based
on 'C', but I would like to treat them generically.

ob[0]->type.doX(); // always ok
ob[0]->type.getInfo(); // always ok
ob[0]->type.getCInfo(); // only ok if type is Class C

I know if I make getBInfo() virtual in the base class then I can
make the above call regardless. But then I have to provide some
return value for it in class A.

If I make it a pure virtual, then I need to provide an implementation
for it in the derived class.

Is there a way to treat a list of objects like this generically without
having to know their
actual type?

It seems kludgey to have something like:

virtual char * getBInfo() {return NULL};

in my base class, but perhaps that's the way it's done.
Using polymorphism you can ask the instance if it is type C.

if ( dynamic_cast<C*>( ob[0] ) != NULL )
dynamic_cast<C*>( ob[0] )->getCInfo(); // only ok if type is Class
C
Jul 24 '07 #4

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

Similar topics

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;...
6
by: Gernot Frisch | last post by:
I want to have a class that provides 2 methods with the same name (do1, do2) that cann be called from a function (Fkt) but Fkt does not know/care about it's type. class base { public:...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
7
by: vaividhya | last post by:
We can have virtual destructors.Why we can't have virtual constructors?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
2
by: babaco | last post by:
Hi, I've come upon a situation where I don't understand what's happening. Basically, I'm implementing a kind of 'chain of responsibility' using some covariant types: #include <iostream>...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.