By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Virtual Constructors and Destructors

P: n/a
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.

Apr 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Alok <ca**********@gmail.com> wrote:

What are virtual constructors and virtual destructors ? Why can't we
have virtual constructors ?


http://groups.google.com/group/comp....622bd42a13aa68

http://groups.google.com/group/comp....6104056841e03b

Please use Google *before* posting.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 14 '06 #2

P: n/a
Alok wrote:
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
Apr 14 '06 #3

P: n/a

Alok wrote:
...[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

Apr 14 '06 #4

P: n/a
Alok wrote:
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.

Apr 14 '06 #5

P: n/a
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();


Rolf Magnus wrote:
Alok wrote:
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.


Apr 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.