Virtual Constructors and Destructors

A

Alok

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 C++ ?
waitng for the answers.
 
B

benben

Alok said:
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 C++ ?
waitng for the answers.

Your second question denies the existence of a virtual constructor which
happens to be the subject in your first question.

Ben
 
B

BigBrian

Alok said:
...[snip]...
waitng for the answers.

Rather demanding attidude. Instead of waiting for somebody to answer
your questions ( which appear to me that they might be homework ), why
not try to find the answer yourself!

-Brian
 
R

Rolf Magnus

Alok said:
hii
Would somebody clear my doubts on following qustions .

What are virtual constructors and virtual destructors ?

The former is something non-existant that people keep asking about. The
second is needed when you want to delete an object of a derived class
through a pointer to a base class.
Why can't we have virtual constructors ?

Because they don't make sense. This topic is raised here at least once per
month, so just search for the last thread about this. Google Groups is your
friend.
What are demerits of inheritence in C++ ?

Not sure what you mean by that.
 
R

ryadav

Virtual constructor:

1. From basic c++ concepts, we know constructors never derive into the
derived classes.
2. virtual functions used only when you need to have different
behaviour of the function in
derived class.
3. You cannot override the constructors and it is used to initialize
the objects.

There is no concept of calling base class constructor which is
overriden in derived class.

class x {
virtual x() {}
}

class y: public x
{
x() {} // you cannot override x defined in x.
}

x *p = new y;
p->x(); it never invokes derived class x defination.


You can write you own class and class member function that behaves like
virtual constructor.

class x {
}

class y: public x {
}

class z: public x {
}


class T {
x*p;

public:
T( int otype ) {
if ( otype == y)
p = new y;
else
p = new z;
}

x* readobject () {
return p;
}

}

T *p = new T( y type );
x* p1 = p-> readobject();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top