-
July 10th, 2010, 01:58 PM
#1
Question regarding virtual inheritance
Hi,
I have the following classes:
Code:
class A
{
public:
virtual bool ZeroBalance() const = 0;
virtual BALANCE operator=(const BALANCE lAmount) = 0;
};
class B : public A
{
public:
virtual void Reserve(const BALANCE lAmount) = 0;
virtual void Commit() = 0;
};
class C : public A
{
virtual bool ZeroBalance() const { return false; }
virtual BALANCE operator=(const BALANCE lAmount) { return 0; }
};
and I need a new class:
class D : public C, public B
{
virtualvoid Reserve(const BALANCE lAmount) { }
virtual void Commit() { }
};
it gives me compilation errors, so I added "virtual" in class B and in class C and still it's not good, it claims ambiguity problems...How can I solve this ??
-
July 10th, 2010, 02:34 PM
#2
Re: Question regarding virtual inheritance
Class A has pure virtual functions that need to be implemented by the inherting class. Class B, same story. Class B doesn't implement the pure virtual functions from class A. Class C is implementing the pure virtual functions from Class A correctly. Class D inherits class C and B. That's not possible because class C and B are both derived from class A, this means that there could be duplicate functions, so the compiler probably doesn 't compile it.
Last edited by Skizmo; July 10th, 2010 at 03:27 PM.
-
July 11th, 2010, 08:35 AM
#3
Re: Question regarding virtual inheritance
I didn't implement the functionality of class A in class B because it's the same functionality as in class C which also derives class A. I wanted to reuse the code of class C.
-
July 11th, 2010, 09:16 AM
#4
Re: Question regarding virtual inheritance
Originally Posted by Shvalb
it gives me compilation errors, so I added "virtual" in class B and in class C and still it's not good, it claims ambiguity problems...How can I solve this ??
Where did you add the virtual keyword? It would be much easier if you could just provide a complete example, instead of a code snippet.
I compiled this using virtual inheritance under MS VS2005 and it compiled with 2 warnings:
Code:
main.cpp(27) : warning C4250: 'D' : inherits 'C::C::ZeroBalance' via dominance
main.cpp(19) : see declaration of 'C::ZeroBalance'
main.cpp(27) : warning C4250: 'D' : inherits 'C::C::operator =' via dominance
main.cpp(20) : see declaration of 'C::operator ='
Originally Posted by Shvalb
I didn't implement the functionality of class A in class B because it's the same functionality as in class C which also derives class A. I wanted to reuse the code of class C.
Inheritance is not the best means to achieve code reuse. Polymorphism is a means to achieve abstraction.
Your design looks peculiar to me, though I can't tell much from class names like A, B, C. Why is B derived from A?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 11th, 2010, 02:50 PM
#5
Re: Question regarding virtual inheritance
it claims ambiguity problems...
Nope. Nothing but warnings with VC++ 8 (aka 2005).
Code:
// 65.cpp
typedef int BALANCE;
class A
{
public:
virtual bool ZeroBalance() const = 0;
virtual BALANCE operator=(const BALANCE lAmount) = 0;
};
class B : public A
{
public:
virtual void Reserve(const BALANCE lAmount) = 0;
virtual void Commit() = 0;
};
class C : public A
{
virtual bool ZeroBalance() const { return false; }
virtual BALANCE operator=(const BALANCE lAmount) { return 0; }
};
class D : public C, public B
{
virtual void Reserve(const BALANCE lAmount) { }
virtual void Commit() { }
};
Code:
D:\Temp\65>cl 65.cpp /c /Wall
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
65.cpp
65.cpp(20) : warning C4100: 'lAmount' : unreferenced formal parameter
65.cpp(25) : warning C4100: 'lAmount' : unreferenced formal parameter
65.cpp(19) : warning C4514: 'C::ZeroBalance' : unreferenced inline function has been removed
65.cpp(20) : warning C4514: 'C::operator =' : unreferenced inline function has been removed
65.cpp(25) : warning C4514: 'D::Reserve' : unreferenced inline function has been removed
65.cpp(26) : warning C4514: 'D::Commit' : unreferenced inline function has been removed
Just never let this ("diamond" inheritance) happen, ever. In case you run into that again, you know your design is wrong.
Last edited by Igor Vartanov; July 11th, 2010 at 02:52 PM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|