UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2004-08-09 12:36 PM

biswajitnanda
Member
From: Bangalore
Registered: 2004-08-03
Posts: 6
Website

Re: About Constructors

Can we have virtual constructors and pure virtual constructors? If yes, what are their use? If no, why....

If anyone has any idea, plz share it with me... I'll be very much obliged...


Biswajit Nanda
S/W Eng, Venture Infotek Global Pvt. Ltd.
Bangalore, India

Offline

#2 2004-08-09 04:03 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: About Constructors

class Base;
class Child;

Base* bla[10];
bla[3]->DoSomething();

Offline

#3 2004-08-10 02:31 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: About Constructors

A constructor can't be virtual, especially not a pure virtual one. As i3839
said, there wouldn't be a point to it. Just think about it, a virtual method is
normally "overwritten" by a method with the same name. That's to make
polimorphism possible. To use this feature the classes have to be derived
from the same base class. But if you do that the constructor of the new
class will automatically call the constructor of its parent within its own.
Even if you don't see it directly, at least the default constructor is called.
Now if that thing would be pure virtual it would transform all derived
classes into abstract base classes. A derived class can't overwrite the
constructors of a base class, so making it virtual would have no point
either.

The destructor is another special case. You have to make it virtual in case
you use polimorphism, else the right destructor of a base class pointer will
not be found. It also can't be pure virtual because it is also called by all
destructors of the derived classes.

If you want to make sure that all derived classes take a special set of
variables to initialize themselves you'll have to define for example a pure
virtual method "init". Then you call their constructors and you can later
use the pointer you got to call this init function.

Offline

#4 2004-08-11 12:44 PM

biswajitnanda
Member
From: Bangalore
Registered: 2004-08-03
Posts: 6
Website

Re: About Constructors

Thanks.....
I m now getting dat....


Biswajit Nanda
S/W Eng, Venture Infotek Global Pvt. Ltd.
Bangalore, India

Offline

Board footer

Powered by FluxBB