473,396 Members | 2,057 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.

Private Virtual inheritance problem.

Hello All,

Im taking an OOP course, in one of the tutorials, there was a frozen
class example, i.e. a class which is impossible to inherit from, the
example was something like:

struct ice__ {
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__'s default c'tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__'s c'tor which is impossible.
};

As I understand, since Frozen virtually inherits ice__, Violation which
inherits Frozen must call ice's c'tor, which is impossible, as ice's
c'tor is in the private part of Frozen.

nevertheless, this example compiles and executes just fine under g++
with ansi / pedantic flags. cl (MS's compiler) eats this as well.

any clue ?
Thanks,
Elad.

Mar 6 '06 #1
12 4182

Elad wrote:
Hello All,

Im taking an OOP course, in one of the tutorials, there was a frozen
class example, i.e. a class which is impossible to inherit from, the
example was something like:

struct ice__ {
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__'s default c'tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__'s c'tor which is impossible.
};

As I understand, since Frozen virtually inherits ice__, Violation which
inherits Frozen must call ice's c'tor, which is impossible, as ice's
c'tor is in the private part of Frozen.

nevertheless, this example compiles and executes just fine under g++
with ansi / pedantic flags. cl (MS's compiler) eats this as well.

any clue ?
Thanks,
Elad.


In structures the default access specifier is public while in classes
default specifier is private.

So, Frozen would compile properly since its calling the default
constructor of ice which is in public section. Now Violation would
automatically call default constructor of Frozen which would be an
error since Frozen's default constructor is in private scope.

So you have just made Frozen as a non-derivable class. I however like
the other step as mentioned in the FAQs to make a class final by
specifying a comment on top of Frozen saying that this class should be
final.

Mar 6 '06 #2

Elad wrote:
Hello All,

Im taking an OOP course, in one of the tutorials, there was a frozen
class example, i.e. a class which is impossible to inherit from, the
example was something like:

struct ice__ {
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__'s default c'tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__'s c'tor which is impossible.
};

As I understand, since Frozen virtually inherits ice__, Violation which
inherits Frozen must call ice's c'tor, which is impossible, as ice's
c'tor is in the private part of Frozen.

nevertheless, this example compiles and executes just fine under g++
with ansi / pedantic flags. cl (MS's compiler) eats this as well.

any clue ?
Thanks,
Elad.


May i know the version of the g++ compiler you are using.
i tried it on gcc 3.2.2, borland 5.5.1 and MS C++ compiler , i got
compilation error in all the cases.

Mar 6 '06 #3
> struct ice__ {
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__'s default c'tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__'s c'tor which is impossible.
};


The above code actually rendered class Frozen TOTALLY unusable, not only
underivable. In fact, unless you haven't copy the exact code here the
compiler must issue you more than one errors, for example the typo of
ice replacing a supposed ice__ and Violation::Violation's call to the
private Frozen::Frozen.

Perhaps you really meant the following:

struct ice__ {
ice__() {};
};

class Frozen : private virtual ice__ {
public: // Frozen is usable
Frozen () {};
};

class Violation : public Frozen {};

Now the most derived class is supposed to make a call to the virtual
base class's constructor. The reason why the compiler doesn't complain
now is that up to this point the compiler has not a single clue whether
class Violation is going to be the most derived class of any object,
because not a single object is created yet.

To ask for an error you need to make an object of Violation. For example:

int main(){
Frozen f; // Ok
Violation v; // Error
}

Hope this will help.

Regards,
Ben
Mar 6 '06 #4
Hello Again,

They was indeed a mistake in the code I gave (class'es ctor's were
declared private), was in a rush, sorry about that.

Ben, The code you suggested Is exactly what I ment, yet, for some
reason it *does* compile :-(

here is a copy paste of my test-code:

#include <iostream.h>

struct ice__ {
ice__ () {
cout << "ice__" << endl;
};
};

class Frozen: private virtual ice__ {
public:
Frozen (void) {cout << "FROZEN" << endl;}
};

class Violation : public Frozen {
public:
Violation (void) { cout << "violation ?" << endl;}
};
int main(){

Violation x;

return 0;

};

This is the output of the executable:
ice__
FROZEN
violation ?
As I understand it should not compile what-so-ever ..
It was compiled using g++ 3.4.4 with no problems.

and with .NET's cl ..

Help.

Mar 6 '06 #5
Everything is OK with your code. You can think about it in following
way:
1. ice__ has public ctor - everything OK.
2. Frozen has public ctor and has access to ice ctor & therefore can
call it - everything OK.
3. Violation has public ctor and has access to Frozen public ctor -
everything OK.
So it is Frozen business how it is going to call ctors of basic
classes. Essentially Valuation can't directly call any methods of
ice__.

Mar 6 '06 #6
All is good but 3 :-)

When a base class is inherited virtualy, it is up to the *most derived*
class to initialize it.

It comes from multiple inheritance where virtual base classes are
"united" hence none of the classes in the inheritance tree will call
the virtual base's ctor but the most derived one - in this case it is
Violation's responsibitly to call ice__'s ctor.

Mar 6 '06 #7
Oops

I think I'd just make ice::ice private and Frozen a friend of ice:

class Frozen;

class ice
{
private:
ice(){}
friend class Frozen;
};

class Frozen: private virtual ice{};
class Violation: public Frozen{};
Ben
Mar 6 '06 #8
Elad wrote:
All is good but 3 :-)

When a base class is inherited virtualy, it is up to the *most derived*
class to initialize it.

It comes from multiple inheritance where virtual base classes are
"united" hence none of the classes in the inheritance tree will call
the virtual base's ctor but the most derived one - in this case it is
Violation's responsibitly to call ice__'s ctor.


Yes, and it does that. What you could do is make ice__'s constructor private
and make Frozen a friend of it. Then Violation can't call it, not even
implicitly, as it does now.

Mar 6 '06 #9

Elad wrote:
All is good but 3 :-)

When a base class is inherited virtualy, it is up to the *most derived*
class to initialize it.

It comes from multiple inheritance where virtual base classes are
"united" hence none of the classes in the inheritance tree will call
the virtual base's ctor but the most derived one - in this case it is
Violation's responsibitly to call ice__'s ctor.


hmm, code compiles/runs fine here, gcc version 2.96 20000731 (Red Hat
Linux 7.1 2.96-85)

Mar 6 '06 #10
Hello Rolf,

I'm still trying to figure the difference between theory and practice
:-)

Mar 8 '06 #11
Elad wrote:
Hello Rolf,

I'm still trying to figure the difference between theory and practice
:-)


In theory, practice and theory are the same. In practice, they are
different.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 8 '06 #12
Ben Pope wrote:
Elad wrote:
Hello Rolf,

I'm still trying to figure the difference between theory and practice
:-)


In theory, practice and theory are the same. In practice, they are
different.


In Germany, we have a saying that you can read from many office walls. Dunno
if it's known in English too, but it goes something like:

If you know everything, and nothing works, that's theory.
If everything works, and nobody knows why, that's practice.
In this room, theory and practice are combined in a perfect way:
Nothing works, and nobody knows why.

Mar 8 '06 #13

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

Similar topics

19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
0
by: news_mail_jpa | last post by:
Hi! I'd like to implement a private interface and I have the choices mentioned in the subject. The implementation using private inheritance is probably cleaner but it also adds some space and...
3
by: vineoff | last post by:
When to do like this: class A { ... }; class B : private A { ... }; Thanks.
6
by: Indraseena | last post by:
Hi friends, Can anybody answer me where exactly is the private inheritance is used? example: class Base { public :
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...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
4
by: zhangyefei.yefei | last post by:
i read book <effective c++>,it tell me that public inheritance means is-a ,and private inheritance means is-implemented-in-terms-of. but today i am puzzled by some strange codes. the...
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
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
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
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
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
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.