473,396 Members | 1,987 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.

Whats the deal with virtual inheritance

I was debating with my friend on how to make a class final as in java,
where it can't be further derived but instatiated. So I search the net
and found to do so. Make a base class which has constructor private.
And a Final class inheriting from it privately.
// Code snippet
class FinalBase
{
FinalBase() {}
friend class Final;
};

class Final : virtual private FinalBase
{
public:
};
class DerivedFinal : public Final
{

};

int main()
{
DerivedFinal df;
}

The above code does not compile and gives error in VC++ as follows
error C2248: 'FinalBase::FinalBase' : cannot access private member
declared in class 'FinalBase'.

If I remove the keyword virtual while inheriting from the FinalBase.
The code compiles without any error ... Any comments on this ???

-Amol
Jul 19 '05 #1
1 2112


Amol Chavan wrote:

I was debating with my friend on how to make a class final as in java,
where it can't be further derived but instatiated. So I search the net
and found to do so. Make a base class which has constructor private.
And a Final class inheriting from it privately.
// Code snippet
class FinalBase
{
FinalBase() {}
friend class Final;
};

class Final : virtual private FinalBase
{
public:
};
class DerivedFinal : public Final
{

};

int main()
{
DerivedFinal df;
}

The above code does not compile and gives error in VC++ as follows
error C2248: 'FinalBase::FinalBase' : cannot access private member
declared in class 'FinalBase'.

If I remove the keyword virtual while inheriting from the FinalBase.
The code compiles without any error ... Any comments on this ???


Yep. What is your question?
Why virtual derivation is crucial in the intent?

Well. If you derive virtual then the most derived class is responsible
for initializing the base class. So in this case DerivedFinal
is responsible for constructing FinalBase. But DerivedFinal can't
do so, because the ctor is private and DerivedFinal is not a friend
of FinalBase.
If you derive Final non-virtual then things change. The initialization
chain is: DerivedFinal has to initialize FinalBase and FinalBase has
to initialize Final. All of this is working, since Final is a friend
of FinalBase.

HTH

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

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

Similar topics

18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
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){;} };
3
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's...
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...
12
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
by: =?Utf-8?B?Zmplcm9uaW1v?= | last post by:
Hi all, As I mentioned in a previous thread (see 'Dbghelp, symbols and templates' in microsoft.public.windbg), we created a powerful symbol engine using dbghelp to dump the contents of the stack...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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...

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.