Can we have "Virtual Constructors"?
Answers were Sorted based on User's Feedback
Answer / rani
No, we cannot have virtual constructor. Because constructor
is usually use to create instance of a class and we cannot
make it virtual.
| Is This Answer Correct ? | 47 Yes | 2 No |
Answer / vadivel
No, we cannot have virtual constructor because at this time
(during object creation or intialization) virtual table not
yet created.
| Is This Answer Correct ? | 24 Yes | 1 No |
Answer / shashank agrawal
No, This is not possible because virtual is used for avoid
overriding and the constructor is called when the object of
that class has been created so in this case overriding is
not possible then to make the constructor as virtual is
useless.so we dont create it virtal.
| Is This Answer Correct ? | 13 Yes | 0 No |
Answer / bharat
People have already posted the answer in brief, however for
a newbie, this might not be enough... Hence posting this
thread...
If you are referring to constructors as in pure C++, then
there is no such concept as "virtual" constructors. A
constructor is responsible for creation of the particular
object, and hence cannot be "virtual".
It cannot be so, because a virtual function allows the
actual function call to be "deeper" in the hierarchy than
the type through which it is being called. for example, you
can have a base pointer through which a virtual call is made
to a derived object's function code (Derived class is
"deeper" than base class")
Since the constructor is responsible for creation of the
object itself, the function call being "deeper" than the
type being created does not make any sense. Hence a
constructor is always "local", and virtual constructor
concept does not exist.
However, in the domain of design patterns, there is such a
concept as Virtual Constructor or Factory Method. You can
find more information on this concept in "Thinking in C++
Volume 2" (A superb and Free ebook by Bruce Eckel) at the
location
http://www.planetpdf.com/developer/article.asp?ContentID=6634
Hope this is useful...
| Is This Answer Correct ? | 9 Yes | 0 No |
Answer / barun
The pointer to Virtual table which keeps the pointer of
Virtual functions is created within constructor. It means
virtual table is created after constructor is called.So,
there won't be any virtual constructor.
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / vighnesh
Yes there is no virtual constructors.I can think a a reason
may be because a constructor is never inherited so there is
no polymorphism etc involved .
| Is This Answer Correct ? | 10 Yes | 9 No |
Answer / meet
Constructor cannot be virtual. Because C++ is statically typed language. It is meaningless to the C++ compiler to create the object polymorphically. But it is possible to achieve behaviour like virtual constructor. This can be done by using factory design pattern.
Visit this link to know more about virtual constructor:
http://www.geeksforgeeks.org/advanced-c-virtual-constructor/
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / kaydee
Of course there is "Virtual Constructor" in design
pattern's world. This is another name of "Factory Method",
a creational design pattern.
| Is This Answer Correct ? | 3 Yes | 4 No |
Answer / nishikant sahu
of course no such concept lie within C++ ,but we can achieve
this effect. Abstract Factory method show this type of effect.
| Is This Answer Correct ? | 4 Yes | 12 No |
What is pointer to array in c++?
How will you call C functions from C ++ and vice-versa?
0 Answers Agilent, Tavant Technologies, Thomson Reuters, Verifone,
What is data hiding c++?
Given a simple program designed to take inputs of integers from 1-1000 and to output the factorial value of that number, how would you test this program? You do not have access to the code. Please be as specific as possible.
What is oop in c++?
Can we delete this pointer in c++?
How can you link a c++ program to c functions?
Should I learn c or c++ first?
What problems might the following macro bring to the application?
Write a program to read the data and evaluate the results of the election. Print all output to the screen. Your output should specify: The total number of votes, the number of valid votes and the number of spoilt votes. The winner(s) of the election. Hint: An appropriate search should be used to determine the winner(s). The votes obtained by each candidate sorted in terms of the number of votes obtained. Hint: An appropriate sort should be used to sort the candidate(s). The Source code should be saved as VotingSystem. Project Input: Candidates’ Names and Numbers 2501 Victor Taylor 2502 Denise Duncan 2503 Kamal Ramdhan 2504 Michael Ali 2505 Anisa Sawh 2506 Carol Khan 2507 Gary Owen Votes 3 1 2 5 4 3 5 3 5 3 2 8 1 6 7 7 3 5 6 9 3 4 7 1 2 4 5 5 1 4 0 Project Output: Invalid vote: 8 Invalid vote: 9 Number of voters: 30 Number of valid votes: 28 Number of spoilt votes: 2 The winner(s): 2503 Kamal Ramdhan 2505 Anisa Sawh Candidate Score 2503 Kamal Ramdhan 6 2505 Anisa Sawh 6 2501 Victor Taylor 4 2504 Michael Ali 4 2502 Denise Duncan 3 2507 Gary Owen 3 2506 Carol Khan 2
Mention the storage classes in c++.
Can a program run without main function?