473,405 Members | 2,287 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,405 software developers and data experts.

Private destructor with virtaul inhertiance

I am trying to understand the following code kindly help me
#include<iostream>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<endl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<endl;}
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"<<endl;}
~drived() {cout << "This is drived destructor"<<endl ;}
};
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::drived1()':
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 2546
* pr********@gmail.com:
I am trying to understand the following code kindly help me
#include<iostream>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<endl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<endl;}
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"<<endl;}
~drived() {cout << "This is drived destructor"<<endl ;}
};
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::drived1()':
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********@gmail.com:
I am trying to understand the following code kindly help me
#include<iostream>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<endl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<endl;}
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"<<endl;}
~drived() {cout << "This is drived destructor"<<endl ;}
};
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::drived1()':
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********@gmail.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
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...
3
by: Arve Sollie | last post by:
class myClass { private: int refCount; ~myClass(); public: myClass(); void incRefCount() { ++refCount; }
5
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...
8
by: Alfonso Morra | last post by:
Is it possible to declare a class destructor as private?
2
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...
23
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....
6
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...
7
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
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.