Private destructor with virtaul inhertiance

P

pragtideep

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 ??
 
A

Alf P. Steinbach

* (e-mail address removed):
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

Alf P. Steinbach

* Alf P. Steinbach:
* (e-mail address removed):

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'.
 
P

pragtideep

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)
 
A

Alf P. Steinbach

* (e-mail address removed):
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

Amal P

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top