468,491 Members | 2,022 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

private destructor in base class

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");
}
};
class B : public A
{
public : ~B()
{
}
};

int main()
{
return 0;
}

And as expected i get a compilation error, saying "cannot access
private member A::~A declared in A"

But when i change the derived class like the following,

class B : public A
{
}

int main()
{
return (0);
}

there is no compilation error, i get an error only when i try to
create an object of the class B. In other words, class B is just an
abstract class. Doesn't the default destructor in B tries to access
the destructor of A and shouldn't that give a compilation error in the
first place, similar to the case where the user provides the
destructor in B?
Dec 22 '07 #1
7 3788
On Dec 22, 1:49*pm, Rahul <sam_...@yahoo.co.inwrote:
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");
* * * * * * * * }

};

class B : public A
{
public : ~B()
* * * * * * * * *{
* * * * * * * * *}

};

int main()
{
* return 0;

}

And as expected i get a compilation error, saying "cannot access
private member A::~A declared in A"

But when i change the derived class like the following,

class B : public A
{

}

int main()
{
*return (0);

}

there is no compilation error, i get an error only when i try to
create an object of the class B. In other words, class B is just an
abstract class. Doesn't the default destructor in B tries to access
the destructor of A and shouldn't that give a compilation error in the
first place, similar to the case where the user provides the
destructor in B?
hi rahul,

Class B in second case didn't have any ctor or dtor method defined, so
compiler doesn't check class A dctor being private.if you define a
ctor/dtor for class B . compiler will throw an error.
Dec 22 '07 #2
On Dec 22, 5:56 pm, contact.me.at.a...@gmail.com wrote:
On Dec 22, 1:49 pm, Rahul <sam_...@yahoo.co.inwrote:
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");
}
};
class B : public A
{
public : ~B()
{
}
};
int main()
{
return 0;
}
And as expected i get a compilation error, saying "cannot access
private member A::~A declared in A"
But when i change the derived class like the following,
class B : public A
{
}
int main()
{
return (0);
}
there is no compilation error, i get an error only when i try to
create an object of the class B. In other words, class B is just an
abstract class. Doesn't the default destructor in B tries to access
the destructor of A and shouldn't that give a compilation error in the
first place, similar to the case where the user provides the
destructor in B?

hi rahul,

Class B in second case didn't have any ctor or dtor method defined, so
compiler doesn't check class A dctor being private.if you define a
ctor/dtor for class B . compiler will throw an error.
Yes i understand that, but is there anyway so that i get a compilation
error for second case? i mean, without defining a constructor (or)
destructor in the derived class...
Dec 22 '07 #3
On Dec 22, 10:03*pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 22, 5:56 pm, contact.me.at.a...@gmail.com wrote:
On Dec 22, 1:49 pm, Rahul <sam_...@yahoo.co.inwrote:
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");
* * * * * * * * }
};
class B : public A
{
public : ~B()
* * * * * * * * *{
* * * * * * * * *}
};
int main()
{
* return 0;
}
And as expected i get a compilation error, saying "cannot access
private member A::~A declared in A"
But when i change the derived class like the following,
class B : public A
{
}
int main()
{
*return (0);
}
there is no compilation error, i get an error only when i try to
create an object of the class B. In other words, class B is just an
abstract class. Doesn't the default destructor in B tries to access
the destructor of A and shouldn't that give a compilation error in the
first place, similar to the case where the user provides the
destructor in B?

Note that the default methods of a class that a compiler can generate
namely: default constructor, destructor, assignment operator, copy
constructor, pair of address-of operators (did I miss anything?), are
only provided by compilers when they are needed (or your code shows a
need of them).

If you explicitly write a destructor, it shows a compilation error.
But if you do not explicitly write it, and you do not create an object
of a class none of the above would be needed and hence compiler does
not generate them. And if the compiler does not generate them, there
is no compilation around them and hence no errors. Those default
members are only generated when they are *needed*. I am not sure if
the standard mandates that or if it is a general compiler
implementation strategy.
>
hi rahul,
Class B in second case didn't have any ctor or dtor method defined, so
compiler doesn't check class A dctor being private.if you define a
ctor/dtor for class B . compiler will throw an error.

Yes i understand that, but is there anyway so that i get a compilation
error for second case? i mean, without defining a constructor (or)
destructor in the derived class
I am not at all sure what you ask for here. What do you mean "is there
anyway so that i get a compilation error for second case"? Create an
object, you will get an error. Sorry, I am not getting the context of
your question. Why would you need that and how would that be helpful
to you.
Dec 22 '07 #4
In article
<3f**********************************@i12g2000prf. googlegroups.com>,
Abhishek Padmanabh <ab****************@gmail.comwrote:
On Dec 22, 10:03*pm, Rahul <sam_...@yahoo.co.inwrote:
I am not at all sure what you ask for here. What do you mean "is there
anyway so that i get a compilation error for second case"? Create an
object, you will get an error. Sorry, I am not getting the context of
your question. Why would you need that and how would that be helpful
to you.
I guessing Rahul wants to catch a /design/ error before the object is
used, by causing the compiler to generate an error message to flag the
undesired inheritance.

Unfortunately it's very difficult to implement "final" class semantics
in C++. Why would you need it anyway?

-dr
Dec 23 '07 #5
On Dec 22, 10:26 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 22, 10:03 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 22, 5:56 pm, contact.me.at.a...@gmail.com wrote:
On Dec 22, 1:49 pm, Rahul <sam_...@yahoo.co.inwrote:
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");
}
};
class B : public A
{
public : ~B()
{
}
};
int main()
{
return 0;
}
And as expected i get a compilation error, saying "cannot access
private member A::~A declared in A"
But when i change the derived class like the following,
class B : public A
{
}
int main()
{
return (0);
}
there is no compilation error, i get an error only when i try to
create an object of the class B. In other words, class B is just an
abstract class. Doesn't the default destructor in B tries to access
the destructor of A and shouldn't that give a compilation error in the
first place, similar to the case where the user provides the
destructor in B?

Note that the default methods of a class that a compiler can generate
namely: default constructor, destructor, assignment operator, copy
constructor, pair of address-of operators (did I miss anything?), are
only provided by compilers when they are needed (or your code shows a
need of them).

If you explicitly write a destructor, it shows a compilation error.
But if you do not explicitly write it, and you do not create an object
of a class none of the above would be needed and hence compiler does
not generate them. And if the compiler does not generate them, there
is no compilation around them and hence no errors. Those default
members are only generated when they are *needed*. I am not sure if
the standard mandates that or if it is a general compiler
implementation strategy.
hi rahul,
Class B in second case didn't have any ctor or dtor method defined, so
compiler doesn't check class A dctor being private.if you define a
ctor/dtor for class B . compiler will throw an error.
Yes i understand that, but is there anyway so that i get a compilation
error for second case? i mean, without defining a constructor (or)
destructor in the derived class

I am not at all sure what you ask for here. What do you mean "is there
anyway so that i get a compilation error for second case"? Create an
object, you will get an error. Sorry, I am not getting the context of
your question. Why would you need that and how would that be helpful
to you.
Well, i just want to implement a final class... and i would like the
compiler to give an error as soon as someone tries to derive from it
instead of waiting for an object to be created. Currently, as you
pointed out, there is no error until someone creates an object.
In all those cases, the developer of the new class (derived from my
final class) can still have some static member functions of the class
and can use the same...

I basically want to stop someone from doing so... why so? thats
altogether another ball game...
Dec 23 '07 #6
On 2007-12-23 13:52, Rahul wrote:
On Dec 22, 10:26 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
>On Dec 22, 10:03 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 22, 5:56 pm, contact.me.at.a...@gmail.com wrote:
On Dec 22, 1:49 pm, Rahul <sam_...@yahoo.co.inwrote:
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");
}
};
class B : public A
{
public : ~B()
{
}
};
int main()
{
return 0;
}
And as expected i get a compilation error, saying "cannot access
private member A::~A declared in A"
But when i change the derived class like the following,
class B : public A
{
}
int main()
{
return (0);
}
there is no compilation error, i get an error only when i try to
create an object of the class B. In other words, class B is just an
abstract class. Doesn't the default destructor in B tries to access
the destructor of A and shouldn't that give a compilation error in the
first place, similar to the case where the user provides the
destructor in B?

Note that the default methods of a class that a compiler can generate
namely: default constructor, destructor, assignment operator, copy
constructor, pair of address-of operators (did I miss anything?), are
only provided by compilers when they are needed (or your code shows a
need of them).

If you explicitly write a destructor, it shows a compilation error.
But if you do not explicitly write it, and you do not create an object
of a class none of the above would be needed and hence compiler does
not generate them. And if the compiler does not generate them, there
is no compilation around them and hence no errors. Those default
members are only generated when they are *needed*. I am not sure if
the standard mandates that or if it is a general compiler
implementation strategy.
hi rahul,
Class B in second case didn't have any ctor or dtor method defined, so
compiler doesn't check class A dctor being private.if you define a
ctor/dtor for class B . compiler will throw an error.
Yes i understand that, but is there anyway so that i get a compilation
error for second case? i mean, without defining a constructor (or)
destructor in the derived class

I am not at all sure what you ask for here. What do you mean "is there
anyway so that i get a compilation error for second case"? Create an
object, you will get an error. Sorry, I am not getting the context of
your question. Why would you need that and how would that be helpful
to you.

Well, i just want to implement a final class... and i would like the
compiler to give an error as soon as someone tries to derive from it
instead of waiting for an object to be created. Currently, as you
pointed out, there is no error until someone creates an object.
In all those cases, the developer of the new class (derived from my
final class) can still have some static member functions of the class
and can use the same...

I basically want to stop someone from doing so... why so? thats
altogether another ball game...
Use Java or C#. None have come up with a good reason to include a final
function in C++ so there is none, but there are a few ways to emulate it
but none of them are perfect. The FAQ list three ways of doing it:
http://www.parashift.com/c++-faq-lit...html#faq-23.11

--
Erik Wikström
Dec 23 '07 #7
On Sat, 22 Dec 2007 00:49:25 -0800, Rahul wrote:
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");
}
};
Note that such construct causes also problems with destructing objects of
class A, not only of classes derived from A.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
A light wife doth make a heavy husband.
-- Wm. Shakespeare, "The Merchant of Venice"
Dec 27 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Stub | last post: by
8 posts views Thread by Dave | last post: by
5 posts views Thread by Christian Meier | last post: by
26 posts views Thread by pmizzi | last post: by
6 posts views Thread by pragtideep | last post: by
23 posts views Thread by Ben Voigt | last post: by
23 posts views Thread by Chris Gordon-Smith | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.