answersLogoWhite

0

Virtual means that decision would be taken at another stage... first base class constructor is called and construction of object cannot be held anonymous. that's y virtual constructors are not possible.

AnswerOne reason is that a constructor cannot be virtual is because it is the constructor's job to fill in the v-table for virtual functions. If a constructor was allowed to be virtual, then you end up with kind of a chicken and egg paradox. In order to call the constructor, the compiler would have to look in the v-table to find where the constructor is located. Unfortunately, it's the constructor's job to place the address of virtual functions (which would include itself) into the v-table. Without RTTI, it would be impossible to resolve which child's constructor to call. In most cases, a virtual helper function that the constructor calls is more then enough.
User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

How do you represent a C plus plus non-pure virtual function in UML?

All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.


What keyword is used to find derivative in C plus plus?

The lazy way is to use a dynamic cast. The correct way is to use virtual functions in the base class. That way you don't ever need to know the derived type -- it simply does what is expected of it.


Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


Does the C plus plus programming language use a virtual machine?

No, it does not. But Microsoft Visual Studio 2008 allows you to connect to a virtual machine and run your projects "sandboxed".


How dynamic binding acheived in c plus plus?

Dynamic binding is achieved via virtual functions and the virtual table that is associated with every class that declares or inherits a virtual function. The virtual table (or v-table) maps every virtual function (including pure-virtual functions) to a function pointer that points to the most-derived overload. This makes it possible to invoke specific behaviour even when the runtime type of the object is unknown to the caller.


Printf and scanf Operators in C and C plus plus?

No, they are functions. Operators are -> or ++or /=


Why functions are not used in c plus plus?

Of course they are used. Both stand-alone and class-member functions are used in C++.


What is a method in c plus plus?

In C++, methods are simply class member functions.


Which desktop application use c plus plus?

There are very few applications of any note that aren't written in C++ (or some combination of C++ and C). Even the Java virtual machine required to interpret Java programs is written in C++.


Why Member functions are not virtual by default?

It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.


What is the difference between C and C plus plus prototypes?

The only difference is that C does not use nor require prototypes. C++ does because all functions and types must at least be declared, if not defined, before they can be used.


C plus plus library functions for string operations?

You can use "string" class in C++ for string operations or you may use c style string functions as well. #include <string> String class in C++ provides all basic function to operate on strings. you may details descriptin at http://www.cplusplus.com/reference/string/string/