private destructor in base class

R

Rahul

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

contact.me.at.arun

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.
 
R

Rahul

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

Abhishek Padmanabh

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.
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.
 
D

Dave Rahardja

Abhishek Padmanabh said:
On Dec 22, 10:03 pm, Rahul <[email protected]> wrote:
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
 
R

Rahul

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.





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...
 
E

Erik Wikström

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-lite/strange-inheritance.html#faq-23.11
 
T

Tadeusz B. Kopec

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top