473,406 Members | 2,549 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 pure virtual functions

Is it possible to realize such an algorithm (this one is not working):

class a1 {

public:

void af () { bf(); }
virtual void bf () = 0;

};

class b1 {

public:

virtual void bf () { }

};

class c1 : public a1, public b1 { };

main () {

c1 x;

}

The general idea is in that if you have one group of classes for one
option and another group of classes for another option you can combine
the general class by any class from the first group and by any class
from the second group. Question is how can functions of one group refer
to functions of another?
Thank you for answers.
Sergey
Oct 31 '05 #1
2 1303
"Sergey" <te*****@yandex.ru> wrote in message
news:dk**********@skeeter.ucdavis.edu...
Is it possible to realize such an algorithm (this one is not working):

class a1 {

public:

void af () { bf(); }
virtual void bf () = 0;

};

class b1 {

public:

virtual void bf () { }

};

class c1 : public a1, public b1 { };

main () {

c1 x;

}

The general idea is in that if you have one group of classes for one
option and another group of classes for another option you can combine
the general class by any class from the first group and by any class
from the second group. Question is how can functions of one group refer
to functions of another?
Thank you for answers.
Sergey


I'm not 100% sure of what you're trying to do, but it looks like some sort
of "mixin" where the b1 class contains the implementation for a1::bf(), but
it does not inherit from a1, and c1 must somehow "combine" the relationship
between the two classes. You can do this with a simple, straightforward
"override and delegate" function:

class c1 : public a1, public b1 {
virtual void bf() {
b1::bf();
}
};

Here, c1::bf() is invoked, and it calls b1::bf().

--
David Hilsee
Oct 31 '05 #2
"Sergey" <te*****@yandex.ru> wrote in message
news:dk**********@skeeter.ucdavis.edu
Is it possible to realize such an algorithm (this one is not working):

class a1 {

public:

void af () { bf(); }
virtual void bf () = 0;

};

class b1 {

public:

virtual void bf () { }

};

class c1 : public a1, public b1 { };

main () {

c1 x;

}

The general idea is in that if you have one group of classes for one
option and another group of classes for another option you can combine
the general class by any class from the first group and by any class
from the second group. Question is how can functions of one group
refer to functions of another?
Thank you for answers.
Sergey


You can give them a common base class and move the virtual function to it:

#include <iostream>
using namespace std;

class Base
{
public:
virtual void bf () = 0;
};
class a1 : virtual public Base
{
public:
void af () { bf(); }
};

class b1 : virtual public Base
{
public:
virtual void bf ()
{
cout << "This is the b1 override of bf\n";
}
};

class c1 : public a1, public b1 { };

int main ()
{
c1 x;
x.af();
}
--
John Carson
Oct 31 '05 #3

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

Similar topics

1
by: jseely13 | last post by:
in c++ when you create a superclass and subclass, there is some way you can define the superclass such that it has an empty/undefined method. any children of this class are required to both have...
2
by: Mattias B | last post by:
Hello! I have s quick question regarding multiple ineritance that I hope that someone will have the time to look at. I have the following classes that I try to compile and use: class A {...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
14
by: junw2000 | last post by:
Is it possible to create an abstract base class without a pure virtual function? Thanks Jack
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...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.