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? 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.
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...
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.
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
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...
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
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" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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"...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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....
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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...
|
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=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
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...
| |