473,408 Members | 2,477 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,408 software developers and data experts.

virtual constructors and distructors...

What is the use of "PURE vitual distructors"?
And why we can not have vitual constructors?

Jun 25 '07 #1
8 2310
Shraddha <sh*************@gmail.comwrote in news:1182812988.480330.318240
@j4g2000prf.googlegroups.com:
What is the use of "PURE vitual distructors"?
Forces a child class to declare their own destructor. Note that even
though it's a pure virtual destructor, you still need to supply a body.
And why we can not have vitual constructors?
See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-20.8

Jun 25 '07 #2
On 2007-06-26 01:09, Shraddha wrote:
What is the use of "PURE vitual distructors"?
If you have a class that you never want to be used as it is, instead you
want the user to always derive from it you can declare the destructor
pure virtual (but provide an implementation). This forces the user to
derive if they want to use the class.

--
Erik Wikström
Jun 26 '07 #3
On 26 Jun, 00:09, Shraddha <shraddhajosh...@gmail.comwrote:
What is the use of "PURE vitual distructors"?
And why we can not have vitual constructors?
what is the use of virtual function anyway?

the difference between pure virtual function and
pure virtual destructor is that you have to write
a body for PV destructor while you don't have to for
PV function. also PV function must be implemented
in the Derived (if you want to instantiate Derived) but
you don't have to implement ~Derive, compiler
generated one will suffice

struct Base
{
virtual ~Base()=0{}
virtual void foo(){}
};

struct Derived : public Base
{
~Derived(){}
void foo(){}
};

int main()
{
Base * p = new Derived;
p->foo();
delete p;
}

p->foo() will call Derived::foo, if Base::foo wasn't
virtual it would call Base::foo

delete p will call Derived destructor, if Base
destructor wasn't virtual it would call Base destructor

since ~Derived will call ~Base you have to provide
a body for ~Base even if it is pure virtual

regards

DS

Jun 26 '07 #4
On 26 Jun, 11:55, dasjotre <dasjo...@googlemail.comwrote:

also, declaring any member pure virtual makes
type non instantiable.
struct Base
{
virtual ~Base()=0{}
virtual void foo(){}
};
Base b; // ERR!
regards

DS

Jun 26 '07 #5
On Jun 26, 7:55 am, dasjotre <dasjo...@googlemail.comwrote:
struct Base
{
virtual ~Base()=0{}
this is a syntax error
it should be

struct Base
{
virtual ~Base()=0;
};
....

Base::~Base() {}
Jun 27 '07 #6
On Jun 27, 12:46 pm, Diego Martins <jose.di...@gmail.comwrote:
On Jun 26, 7:55 am, dasjotre <dasjo...@googlemail.comwrote:
struct Base
{
virtual ~Base()=0{}

this is a syntax error

it should be

struct Base
{
virtual ~Base()=0;};

...

Base::~Base() {}
Good point. Also why destructor can be non virtual? Is there any use
for that? I have never seen a real world scenario where destructors
shoudl not be virtual.

On the other hand, constructors can't be virtual as was already
pointed out. Except for one compiler called Borland C++ Builder. That
compiler allowed to declare virtual constructors, although the meaning
was different of what you could expect.

Normally C++ constructors can't make virtual function calls, because
the the VMT (virtual method table) is not fully built yet, leading to
subtle and unexpected bugs. In C++ Builder, you could declare your
constructors to be virtual, meaning that before executing the
constructor, the VMT would be fully built and therefore the virtual
methods would be called as appropiate.

Cheers,
Guillermo.

Jun 28 '07 #7
On Jun 28, 6:13 am, Guillermo Schwarz <guillermo.schw...@gmail.com>
wrote:
On Jun 27, 12:46 pm, Diego Martins <jose.di...@gmail.comwrote:
On Jun 26, 7:55 am, dasjotre <dasjo...@googlemail.comwrote:
struct Base
{
virtual ~Base()=0{}
this is a syntax error
it should be
struct Base
{
virtual ~Base()=0;};
...
Base::~Base() {}
Good point. Also why destructor can be non virtual? Is there any use
for that? I have never seen a real world scenario where destructors
shoudl not be virtual.
Really? What about structures which are shared with C? Putting
a vptr into the structure isn't going to please the C compiler
any. For that matter, making the destructor virtual for
something like complex<floatcould double the size of the
object. An important consideration for people working with
large arrays of the thing. In general, for classes with value
semantics, there's no reason for the destructor to be virtual,
and every reason for it not to be.

Once a class has virtual functions, of course, the question
changes. The vptr is already there, so making the destructor
virtual doesn't change anything in this respect. And the
presence of virtual functions means that there is a very high
probability that inheritance will be involved somewhere, and
that a virtual destructor will be needed. On the other hand,
making the virtuality of the destructor depend on whether other
virtual functions are present or not isn't very orthogonal, to
say the least, and is another complication. And IMHO, C++
already has enough special cases, and doesn't really need
another one.
On the other hand, constructors can't be virtual as was already
pointed out. Except for one compiler called Borland C++ Builder. That
compiler allowed to declare virtual constructors, although the meaning
was different of what you could expect.
Normally C++ constructors can't make virtual function calls,
Normally, the can, and in some cases, do.
because the the VMT (virtual method table) is not fully built
yet,
Sorry, but the vtbl is always complete, according to the dynamic
type of the object.
leading to
subtle and unexpected bugs.
Avoiding subtle and unexpected bugs is precisely the reason why
the dynamic type evolves. Calling a function on a type whose
constructor hasn't yet started running is a sure recepe for
disaster. Look at all the NullPointerException it causes in
Java.
In C++ Builder, you could declare your
constructors to be virtual, meaning that before executing the
constructor, the VMT would be fully built and therefore the virtual
methods would be called as appropiate.
You mean, the call would dispatch to a function whose this
pointer pointed to raw memory? Thank God C++ doesn't support an
idiocy like that.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 28 '07 #8
On Jun 28, 7:55 am, James Kanze <james.ka...@gmail.comwrote:
On Jun 28, 6:13 am, Guillermo Schwarz <guillermo.schw...@gmail.com>
wrote:
On Jun 27, 12:46 pm, Diego Martins <jose.di...@gmail.comwrote:
On Jun 26, 7:55 am, dasjotre <dasjo...@googlemail.comwrote:
struct Base
{
virtual ~Base()=0{}
this is a syntax error
it should be
struct Base
{
virtual ~Base()=0;};
...
Base::~Base() {}
Good point. Also why destructor can be non virtual? Is there any use
for that? I have never seen a real world scenario where destructors
shoudl not be virtual.

Really? What about structures which are shared with C? Putting
a vptr into the structure isn't going to please the C compiler
any. For that matter, making the destructor virtual for
something like complex<floatcould double the size of the
object. An important consideration for people working with
large arrays of the thing. In general, for classes with value
semantics, there's no reason for the destructor to be virtual,
and every reason for it not to be.

Once a class has virtual functions, of course, the question
changes. The vptr is already there, so making the destructor
virtual doesn't change anything in this respect. And the
presence of virtual functions means that there is a very high
probability that inheritance will be involved somewhere, and
that a virtual destructor will be needed. On the other hand,
making the virtuality of the destructor depend on whether other
virtual functions are present or not isn't very orthogonal, to
say the least, and is another complication. And IMHO, C++
already has enough special cases, and doesn't really need
another one.
FYI, g++ has a pretty useful compiler warning when a class without a
virtual destructor have other virtual functions
:)

Diego

Jun 29 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gianni Mariani | last post by:
In the code below, controller::controller() is never invoked, however, it appears there is no way to make a compile-time rule that this should not happen. The code below seems to make compilers...
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...
7
by: vaividhya | last post by:
We can have virtual destructors.Why we can't have virtual constructors?
5
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in...
3
by: ennio | last post by:
Hi, i have a doubt i can't solve right now. Your help will be appreciated. I am studying constructors/copy constructors/destructors. I created this a little class (see below). If i comment the...
6
by: Carl R. Davies | last post by:
I was reading this link http://www.icce.rug.nl/documents/cplusplus/cplusplus14.html#l198 heading "14.10 Virtual Constructors" I am struggling to understand the issue the author is trying to...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
4
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
4
by: Rahul | last post by:
Hi Everyone, I understand that the constructors can't be virtual and parashift has the following example, to have an workaround for the constructors to be virtual, class Shape { public:...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.