473,836 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why no virtual constructor in C++ ?

2 New Member
Hi,
I want to know about virtual constructor in C++.
I know there is no such feature in C++. But why????
What are the reasons of that????

Thanks & Regards
Krishna
Apr 14 '06 #1
7 36922
179004
1 New Member
A constructor can not be virtual because at the time when constructor is invoked the vittual table would not be available in the memory .hence we can not have virtual constructor .
Aug 26 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
Also if you think logically about how constructors work and what the meaning/usage of a virtual function is in C++ then you will realise that a virtual constructor would be meaningless in C++.

Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class, however the constructor is called when the objected is created, at that time you can not be creating a sub-class of the class you must be creating the class so there would never be any need to declare a constructor virtual.
Aug 26 '06 #3
venkataramana
1 New Member
Hi,
I want to know about virtual constructor in C++.
I know there is no such feature in C++. But why????
What are the reasons of that????

Thanks & Regards
Krishna
When ever we are creating an object for a class using new operator, then at that point of time we should know what type of object we are creating, the new operator will invoke the constructor so if we don't know the type of the constructor then it is not possible to create that so because of this resone virtual constructor in not possible.
Apr 4 '07 #4
sdprogrammer
1 New Member
Actually the virtual method table exists for each class not or the instances of the Class. Many languages do Have virtual Constructors. Delphi does for Example (I think C# does also). It allows you to call the proper constructor on a class reference.

if it existed in C++ it would be kind of like this

class Dog
{
virtual Dog();
....
}
class Poodle: public Dog
{
virtual Poodle();
...
}

class Rotweiler: public Dog
{
virtual Rotweiler();
...
}
class UglyPoodle:publ ic Poodle
{
virtual UglyPoodle();
//...
}

typdef DogClass = class of Dog; //thats Delphi'ish notation

Dog* DogFactory(DogC lass DogKind)
{
return new DogKind;
}

int main()
{
Dog* D = DogFactory(Pood le);
}

//this of course can be done without class references but in some cases this is much easier and more elegant. consider a complicated class hierarchy of Controls, WindowedControl s, Buttons etc. you can easily construct the right kind of Object without complicated switch statements or anything
}
May 24 '07 #5
jijijacob
7 New Member
i just wanted to know "What is virtual table?"
Dec 18 '07 #6
Meetee
931 Recognized Expert Moderator Contributor
i just wanted to know "What is virtual table?"
Whenever a program has a virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address.

For more search on google.
Dec 18 '07 #7
jijijacob
7 New Member
can you tell me the creation and executing procedure for functions other than virtual functions?
Dec 19 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
374
by: uday | last post by:
can u explain me the concept of virtual constructor. Does such concept exists in c++
5
2035
by: Devika | last post by:
hi, I understand the need of virtual destructor.but virtual constructor is not supported in c++.but basically why we need to have virtual constructor?? thx in advance Devika
8
4048
by: Asfand Yar Qazi | last post by:
Hi, I have the following header file in my 'everything useful I think of in one place' library: ============= BEGIN CODE SNIPPET =========== /** All classes that derive from this obtain a 'virtual constructor' - ie if the 'clone()' method is called on a polymorphic type, an object of the same (unknown) type is returned. */
16
3764
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
4
1664
by: ramesh | last post by:
Why do we have a virtual destructor and why cant we have a virtual constructor?
8
2182
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A virtual destructor virtual void draw() = 0; // A pure virtual function virtual void move() = 0; ...
1
1467
by: Christof Warlich | last post by:
Hi all, I'm using the virtual constructor ideom to hide implementation details of services from an application that uses these services, i.e.: // serviceInterface.h class SomeService { public: static SomeService *create(void); virtual void useService(void) = 0;
1
3478
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } // A virtual destructor
0
9656
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10527
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10575
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9358
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.