473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 4255
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.