473,396 Members | 1,671 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,396 software developers and data experts.

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 1690
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.********@comAcast.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.********@comAcast.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.********@comAcast.netschrieb 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.********@comAcast.netschrieb 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...@comAcast.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*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
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...@comAcast.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...@comAcast.netwrote:
James Kanze wrote:
On Jun 8, 8:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.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*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
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
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...
10
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...
3
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...
8
by: Ioannis Vranos | last post by:
Why this does not compile: class Sealed { friend class Fred; Sealed() { } }; class Fred: virtual Sealed
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
5
by: mihai | last post by:
I have a situation witch is like this: // A.h class A { class AA { }; };
1
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
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...
1
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
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
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
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.