473,803 Members | 4,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private destructor with virtaul inhertiance

I am trying to understand the following code kindly help me
#include<iostre am>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<en dl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<e ndl;}
virtual void test1() {cout << "i am base virtual method " <<endl;}
};
class drived : virtual public base { // Line 11
remove the virtual keyword
public:
drived () {cout << "I am derived contructor"<<en dl;}
~drived() {cout << "This is drived destructor"<<en dl ;}
};
class drived1 : public drived {
};
int main () {
// drived* b1 ;
base* b1 ;
b1 =new drived1;
// delete b1;
return 1;
}

g++ privdest.cpp
privdest.cpp: In constructor `drived1::drive d1()':
privdest.cpp:5: `base::~base()' is private
privdest.cpp:23 : within this context

does not compile as i expected
but when i removed the virtual from line number 11 i was able to
compile it why so ??

Mar 16 '06 #1
6 2569
* pr********@gmai l.com:
I am trying to understand the following code kindly help me
#include<iostre am>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<en dl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<e ndl;}
virtual void test1() {cout << "i am base virtual method " <<endl;}
};
class drived : virtual public base { // Line 11
remove the virtual keyword
public:
drived () {cout << "I am derived contructor"<<en dl;}
~drived() {cout << "This is drived destructor"<<en dl ;}
};
class drived1 : public drived {
};
int main () {
// drived* b1 ;
base* b1 ;
b1 =new drived1;
// delete b1;
return 1;
}

g++ privdest.cpp
privdest.cpp: In constructor `drived1::drive d1()':
privdest.cpp:5: `base::~base()' is private
privdest.cpp:23 : within this context

does not compile as i expected
but when i removed the virtual from line number 11 i was able to
compile it why so ??


Sounds like a compiler bug. Except that Comeau Online gives about the
same message. On the third hand, using a private /constructor/ in a
virtual base class, plus friendship declaration, is a common idiom, and
constructor, destructor, shouldn't make any difference.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 16 '06 #2
* Alf P. Steinbach:
* pr********@gmai l.com:
I am trying to understand the following code kindly help me
#include<iostre am>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<en dl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<e ndl;}
virtual void test1() {cout << "i am base virtual method " <<endl;}
};
class drived : virtual public base { // Line 11
remove the virtual keyword
public:
drived () {cout << "I am derived contructor"<<en dl;}
~drived() {cout << "This is drived destructor"<<en dl ;}
};
class drived1 : public drived {
};
int main () {
// drived* b1 ;
base* b1 ;
b1 =new drived1;
// delete b1;
return 1;
}

g++ privdest.cpp
privdest.cpp: In constructor `drived1::drive d1()':
privdest.cpp:5: `base::~base()' is private
privdest.cpp:23 : within this context

does not compile as i expected
but when i removed the virtual from line number 11 i was able to
compile it why so ??


Sounds like a compiler bug. Except that Comeau Online gives about the
same message. On the third hand, using a private /constructor/ in a
virtual base class, plus friendship declaration, is a common idiom, and
constructor, destructor, shouldn't make any difference.


Uh oh! I'm really glad nobody else have replied in this thread!

Sorry, I did not see the second level of inheritance, 'drived' and
'drived1' being very similar names.

In the future, to avoid similar bugs and "unexpected " behavior:

* Use /meaningful/ names.

For virtual inheritance constructors and destructors are invoked by the
most derived class, which in your case is not a friend.

Friendship has been granted to 'drived', not to 'drived1'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 16 '06 #3
Yeah i agree with you , i too expect code not to compile .But when i
remove the virtual keyword from line number 11 the code compiled and
gave the output .So the question is why it happed to compile (when
virtual was removed).
I am using linux gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-34)

Mar 16 '06 #4
* pr********@gmai l.com:
Yeah i agree with you , i too expect code not to compile .But when i
remove the virtual keyword from line number 11 the code compiled and
gave the output .So the question is why it happed to compile (when
virtual was removed).


Unless there is more names-differing-in-one-nearly-invisible-letter
stuff hidden in your code, there shouldn't be any problem with
non-virtual inheritance. ~base is accessible to drived, and ~drived is
accessible to drived1, which is all that's needed. What more do you
think should be needed, and why?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 16 '06 #5
Why it work when virtual is not there?
A.)
class "derived" is a friend of class "base". When you derive class
"derived" from class "base", the class "derived" can access all private
members in class "base" since class "derived" is the friend of class
"base". Now when you derive the third class ie class "derived1" from
class "derived" it will work because class derived is friend of class
base and it can access the destructor.

Why it does not work when virtual is used?
A.)
When the class "derived" is virtully derived from class "base" the
class "derived" will not look either the constructor or the destructer
of class "base". This task will be given to the third class ie class
"derived1". Now when the class "derived1" is instanstiated (ie when you
create the object of derived1) it will try to find the destructor of
class "base". But the destructor of class "base" is private and so
"derived1" will not be able to find that. So the compiler will produce
a error.

Hope the idea is clear for you,
Best Regards,
Amal P.

Mar 16 '06 #6
Thanx Amal i got the concept

Mar 16 '06 #7

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

Similar topics

3
21395
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG rajeshgarg@opussoft.com
3
8828
by: Arve Sollie | last post by:
class myClass { private: int refCount; ~myClass(); public: myClass(); void incRefCount() { ++refCount; }
5
6434
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using std::endl;
8
7484
by: Alfonso Morra | last post by:
Is it possible to declare a class destructor as private?
2
3990
by: Rob Long | last post by:
Hi there Is there any way to access private variables directly from within a priviliged function? I have a situation where the priviliged function's execution context contains variables of the same name as the parent context, but I want direct access to the parent context's variable. E.g. I would like to be able to do this... function Point()
23
2612
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
6
2207
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made constructor as private and destructor as public. class Single { private:
7
4295
by: Rahul | last post by:
Hi Everyone, I was trying to implement a final class and i start having the destructor of the final class as private, class A { ~A() { printf("destructor invoked\n");
23
2656
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class Add_Molecule_Req: public Action_Request{ // ...... };
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
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
10316
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
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
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
5500
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...
1
4275
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
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.