468,783 Members | 1,590 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,783 developers. It's quick & easy.

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 2317
* 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rajesh Garg | last post: by
3 posts views Thread by Arve Sollie | last post: by
5 posts views Thread by Christian Meier | last post: by
8 posts views Thread by Alfonso Morra | last post: by
23 posts views Thread by Ben Voigt | last post: by
7 posts views Thread by Rahul | last post: by
23 posts views Thread by Chris Gordon-Smith | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.