473,779 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance Problem (MSVC 6)

Hi NG,

I have an inheritance like this:

class a_interface
{
virtual bool x() = 0;
};

class a_version_1 : public a_interface
{
virtual bool x() {return true;}
}

class a_version_2: public a_interface
{
virtual bool x() {return false;}
}

class b_interface: public a_interface
{
};

class b_derived: public a_interface, public a_version_1
{
};

class b_interface is never instantiated, only its derived classes are (there
are lots of them, some need a_version1, some a_version2).
When I try to create an instance of b_derived, I get the error "pure virtual
function not defined". Why doesn't it take bool x() from a_version_1?
Is this a bug in Visual Studio 6?
What can I do about it, if I need to access a_interface from within
b_interface, other than re-implementing a_version_x in every derived class?

TIA
Hans-Dieter Dreier
Jun 8 '07 #1
8 1718
Hans-Dieter Dreier wrote:
Hi NG,

I have an inheritance like this:

class a_interface
{
virtual bool x() = 0;
};

class a_version_1 : public a_interface
{
virtual bool x() {return true;}
}

class a_version_2: public a_interface
{
virtual bool x() {return false;}
}

class b_interface: public a_interface
{
};

class b_derived: public a_interface, public a_version_1
I think you meant

class b_derived: public b_interface, public a_version_1
{
};

class b_interface is never instantiated, only its derived classes are
(there are lots of them, some need a_version1, some a_version2).
When I try to create an instance of b_derived, I get the error "pure
virtual function not defined". Why doesn't it take bool x() from
a_version_1?
Is this a bug in Visual Studio 6?
No.
What can I do about it, if I need to access a_interface from within
b_interface, other than re-implementing a_version_x in every derived
class?
First of all, your 'a_interface' should probably be derived from
*virtually*. Have you tried that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #2
----- Original Message -----
From: "Victor Bazarov" <v.********@com Acast.net>
Newsgroups: comp.lang.c++
Sent: Friday, June 08, 2007 7:35 PM
Subject: Re: Inheritance Problem (MSVC 6)
class b_derived: public a_interface, public a_version_1

I think you meant

class b_derived: public b_interface, public a_version_1
Sure. Sorry for the typo.
What can I do about it, if I need to access a_interface from within
b_interface, other than re-implementing a_version_x in every derived
class?

First of all, your 'a_interface' should probably be derived from
*virtually*. Have you tried that?
Yes. No difference. AFAIK the purpose of virtual inheritance is just to
avoid duplication of data members, so that's no surprise to me.

Do you know how the C++ standard would handle such a situation?
Jun 8 '07 #3
Hans-Dieter Dreier wrote:
----- Original Message -----
From: "Victor Bazarov" <v.********@com Acast.net>
Newsgroups: comp.lang.c++
Sent: Friday, June 08, 2007 7:35 PM
Subject: Re: Inheritance Problem (MSVC 6)
>>class b_derived: public a_interface, public a_version_1

I think you meant

class b_derived: public b_interface, public a_version_1

Sure. Sorry for the typo.
>>What can I do about it, if I need to access a_interface from within
b_interface , other than re-implementing a_version_x in every derived
class?

First of all, your 'a_interface' should probably be derived from
*virtually*. Have you tried that?

Yes. No difference. AFAIK the purpose of virtual inheritance is just
to avoid duplication of data members, so that's no surprise to me.

Do you know how the C++ standard would handle such a situation?
In order to make a class non-abstract all pure virtual functions it
inherits have to have final non-pure overriders. Overriding only
works *directly* in the hierarchy. You cannot expect your class'
uncle to override your class' father's pure functions.

struct ABC {
virtual void foo() = 0;
};

struct CC : ABC {
void foo() {}
};

struct YAABC : ABC {
// here you have one inherited virtual function
// virtual void foo() = 0;
// and it's pure
};

struct MYCC : YAABC, CC {
// here you have two inherited functions, one
// comes from YAABC and its pure and the other
// comes from CC, and it's OK.
};

The language cannot decide for you what MYCC::foo should do. You
actually have two virtual functions inherited, and one of them is
pure that does not have the final overrider. See subclause 10.3
for the actual rules involved. Paragraph 10 shows the situation
similar to yours (no unambiguous final overrider).

To disambiguate the behaviour you have to introduce the final
overrider into 'MYCC'. What it does is up to you, but the most
acceptable way is

struct MYCC : YAABC, CC {
void foo() { return CC::foo(); }
};

That cannot be done automatically, it would generate more problems
than it would solve.

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

"Victor Bazarov" <v.********@com Acast.netschrie b im Newsbeitrag
news:f4******** **@news.datemas .de...

[snip]
To disambiguate the behaviour you have to introduce the final
overrider into 'MYCC'. What it does is up to you, but the most
acceptable way is

struct MYCC : YAABC, CC {
void foo() { return CC::foo(); }
};

That cannot be done automatically, it would generate more problems
than it would solve.
I already suspected that.
Maybe I'll have to resort to a (shudder!) preprocessor macro to save me a
lot of typing.

Thanks a lot for the quick response anyway !
Hans-Dieter Dreier
Jun 8 '07 #5
Hans-Dieter Dreier wrote:
"Victor Bazarov" <v.********@com Acast.netschrie b im Newsbeitrag
news:f4******** **@news.datemas .de...

[snip]
>To disambiguate the behaviour you have to introduce the final
overrider into 'MYCC'. What it does is up to you, but the most
acceptable way is

struct MYCC : YAABC, CC {
void foo() { return CC::foo(); }
};

That cannot be done automatically, it would generate more problems
than it would solve.

I already suspected that.
Maybe I'll have to resort to a (shudder!) preprocessor macro to save
me a lot of typing.
I doubt that it would actually save you anything. It would definitely
make things more unclear.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #6
On Jun 8, 8:02 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Hans-Dieter Dreier wrote:
In order to make a class non-abstract all pure virtual functions it
inherits have to have final non-pure overriders. Overriding only
works *directly* in the hierarchy. You cannot expect your class'
uncle to override your class' father's pure functions.
struct ABC {
virtual void foo() = 0;
};
struct CC : ABC {
void foo() {}
};
struct YAABC : ABC {
// here you have one inherited virtual function
// virtual void foo() = 0;
// and it's pure
};
struct MYCC : YAABC, CC {
// here you have two inherited functions, one
// comes from YAABC and its pure and the other
// comes from CC, and it's OK.
};
The language cannot decide for you what MYCC::foo should do. You
actually have two virtual functions inherited, and one of them is
pure that does not have the final overrider. See subclause 10.3
for the actual rules involved. Paragraph 10 shows the situation
similar to yours (no unambiguous final overrider).
To disambiguate the behaviour you have to introduce the final
overrider into 'MYCC'. What it does is up to you, but the most
acceptable way is
struct MYCC : YAABC, CC {
void foo() { return CC::foo(); }
};
That cannot be done automatically, it would generate more problems
than it would solve.
The more classical solution, I think, would be to use virtual
inheritance. If there is only one instance of ABC (because CC
and YAABC both inherit virtually from it), then the function is
implemented, and there is no problem.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #7
James Kanze wrote:
On Jun 8, 8:02 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>Hans-Dieter Dreier wrote:
>In order to make a class non-abstract all pure virtual functions it
inherits have to have final non-pure overriders. Overriding only
works *directly* in the hierarchy. You cannot expect your class'
uncle to override your class' father's pure functions.
> struct ABC {
virtual void foo() = 0;
};
> struct CC : ABC {
void foo() {}
};
> struct YAABC : ABC {
// here you have one inherited virtual function
// virtual void foo() = 0;
// and it's pure
};
> struct MYCC : YAABC, CC {
// here you have two inherited functions, one
// comes from YAABC and its pure and the other
// comes from CC, and it's OK.
};
>The language cannot decide for you what MYCC::foo should do. You
actually have two virtual functions inherited, and one of them is
pure that does not have the final overrider. See subclause 10.3
for the actual rules involved. Paragraph 10 shows the situation
similar to yours (no unambiguous final overrider).
>To disambiguate the behaviour you have to introduce the final
overrider into 'MYCC'. What it does is up to you, but the most
acceptable way is
> struct MYCC : YAABC, CC {
void foo() { return CC::foo(); }
};
>That cannot be done automatically, it would generate more problems
than it would solve.

The more classical solution, I think, would be to use virtual
inheritance. If there is only one instance of ABC (because CC
and YAABC both inherit virtually from it), then the function is
implemented, and there is no problem.
That's what I suggested first. For some reason the OP replied
that it didn't work. Old compiler, maybe?...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #8
On Jun 9, 1:54 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
James Kanze wrote:
On Jun 8, 8:02 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Hans-Dieter Dreier wrote:
[...]
The more classical solution, I think, would be to use virtual
inheritance. If there is only one instance of ABC (because CC
and YAABC both inherit virtually from it), then the function is
implemented, and there is no problem.
That's what I suggested first. For some reason the OP replied
that it didn't work. Old compiler, maybe?...
Pre-CFront 2.1? More likely he made a mistake somewhere (maybe
only declared one of the inheritances virtual).

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 9 '07 #9

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

Similar topics

2
4381
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
10
1891
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of that derived class. However, this information will be differenet for each derived class. Now, if I define SharedInformation as a static member of the base class, then SharedInformation cannot vary across the different derived
3
1477
by: Gianni Mariani | last post by:
In the code below, controller::controller() is never invoked, however, it appears there is no way to make a compile-time rule that this should not happen. The code below seems to make compilers complain that controller::controller() is private, even though it is never used. What do others do to work-around this ? I suppose I can simply not implement controller::controller(), that way I get a linking error if there exists errant code....
8
437
by: Ioannis Vranos | last post by:
Why this does not compile: class Sealed { friend class Fred; Sealed() { } }; class Fred: virtual Sealed
10
2494
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
5
1252
by: mihai | last post by:
I have a situation witch is like this: // A.h class A { class AA { }; };
1
2001
by: David Welch | last post by:
Hi, I have a bit of code where I am relying on empty base member optimisation. The bit of code is below: template<typename Enum> struct EncodePrefix { template<Enum e> struct Apply
3
4552
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
1
3848
by: Dave Rahardja | last post by:
MSVC seems to have a tough time with this program: struct A { struct L {}; }; struct B: private A::L
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7478
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.