472,809 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

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 4197
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
8
by: Dave | last post by:
Hello all, Suppose that derived inherits privately from base. A base pointer may not be made to point at a derived object in this case. I understand that is exactly what is supposed to happen...
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...
20
by: frs | last post by:
For memory economization, I need to get rid if the virtual destructor. Under this constraint I get caught up in a design, where I need to call a destructor of a derived class from a base class....
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
6
by: pragtideep | last post by:
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...
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....
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 ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.