Error Constructors Cannot Be Declared Virtual
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow
Can Constructors Be Virtual
the company Business Learn more about hiring developers or posting ads with us Stack Overflow virtual constructor c++ example Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 virtual constructor and destructor in c++ million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why do we not have a virtual constructor in C++? up vote 144 down vote favorite 57 Why does
Why Virtual Constructor Is Not Allowed In C++
C++ not have a virtual constructor? c++ constructor virtual-functions share|improve this question edited Feb 6 '12 at 8:38 nitin_cherian 2,130134388 asked Apr 9 '09 at 8:46 Arjun 764275 add a comment| 18 Answers 18 active oldest votes up vote 140 down vote Hear it from the horse's mouth:). From Bjarne Stroustrup's C++ Style and Technique FAQ Why don't we have virtual constructors? A virtual call is a mechanism to get work done
Why Constructor Cannot Be Declared Final
given partial information. In particular, "virtual" allows us to call a function knowing only any interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a "call to a constructor" cannot be virtual. The FAQ entry goes on to give the code for a way to achieve this end without a virtual constructor. share|improve this answer edited Feb 13 '15 at 10:53 stefan 6,75332863 answered Apr 9 '09 at 8:54 aJ. 20.8k1564105 add a comment| up vote 106 down vote Virtual functions basically provide polymorphic behavior. That is, when you work with an object whose dynamic type is different than the static (compile time) type with which it is referred to, it provides behavior that is appropriate for the actual type of object instead of the static type of the object. Now try to apply that sort of behavior to a constructor. When you construct an object the static type is always the same as the actual object type since: To construct an object, a constructor needs the exact type of the object it is to create [...] Furthermore [...]you cannot have a pointer to a constructor (Bjarne Stroustup (P424 The C++ Programming
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn can destructor be virtual more about Stack Overflow the company Business Learn more about hiring developers or
Why Constructor Cannot Be Virtual In C++
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community virtual copy constructor in c++ Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up A constructor cannot be virtual up vote 2 down http://stackoverflow.com/questions/733360/why-do-we-not-have-a-virtual-constructor-in-c vote favorite 4 In one of the C++ tutorials in internet, i found out the below description on why a constructor cannot be virtual We cannot declare a virtual constructor. We should specify the exact type of the object at compile time, so that the compiler can allocate memory for that specific type. Is this description correct ? I am getting confused particularly with the phrase: so that http://stackoverflow.com/questions/8506938/a-constructor-cannot-be-virtual the compiler can allocate memory for that specific type. c++ constructor virtual-functions share|improve this question edited Dec 14 '11 at 15:21 mkb 9,2981944 asked Dec 14 '11 at 15:18 nitin_cherian 2,130134388 @mkb. Thats for the edit :) –nitin_cherian Dec 14 '11 at 15:21 3 The explanation is half right, half bogus. A constructor can't be virtual because it simply wouldn't make sense. –Fred Foo Dec 14 '11 at 15:24 "so that the compiler can allocate memory for that specific type" is more or less gibberish. We have to specify the type when we create the object, because every object has a type. (Of course, as part of creating the object, some memory has to be allocated depending on the size of that type; but that's not really relevant to specifying the object's type). –Mike Seymour Dec 14 '11 at 15:52 1 @LinuxPenseur: Yes, in C++ objects can only be created when the type is known at compile time. Objects are only created by declarations (which include the type), and new expressions (which also include the type). –Mike Seymour Dec 14 '11 at 16:05 2 @LinuxPenseur: No, it's determined at compile time by the programmer specifying
Years Ago I haven't noticed this until I tried it today and boom! Compiler complained: error: constructors cannot be declared virtual Why then Constructor is not okay https://www.daniweb.com/programming/software-development/threads/315527/why-can-t-we-have-virtual-constructor-but-we-have-virtual-destructor while destructor is, in such as class as this? Thanks class MyAbstractClass{ public: virtual MyAbstractClass(std::string username, std::string password)=0; virtual ~MyAbstractClass(std::string username, std::string password)=0; virtual void someOtherMethodsHere()=0; } Me in nutshell: Man that loves Jesus and explores science & technology! --Save me from screaming, don't use $_REQUEST: Use explicitly $_POST or $_GET *"Try to be expert in everything and you in c++ become expert in nothing"* ~ Me Find me Here Stefano Mtangoo 441 3,730 posts since Jun 2007 Community Member 5Contributors 5Replies 7Views 6 YearsDiscussion Span 6 Years Ago Last Post by Stefano Mtangoo 0 Valaraukar 4 6 Years Ago It would be useless to have a virtual constructor in C++. Having a virtual constructor would mean that you could cannot be declared override the class constructor with that of a derived class. Constructors are called at the point of object creation and hence when constructing you cannot be creating a sub-class of the class but the class itself. 0 Narue 5,707 6 Years Ago http://www2.research.att.com/~bs/bs_faq2.html#virtual-ctor 0 np complete 8 6 Years Ago I haven't noticed this until I tried it today and boom! Compiler complained: error: constructors cannot be declared virtual Why then Constructor is not okay while destructor is, in such as class as this? Thanks class MyAbstractClass{ public: virtual MyAbstractClass(std::string username, std::string password)=0; virtual ~MyAbstractClass(std::string username, std::string password)=0; virtual void someOtherMethodsHere()=0; } Look up C++ for dummies..good for basic in C++, it helped me a lot. 1 Featured Reply firstPerson 761 6 Years Ago >>Look up C++ for dummies HAHAHAHAHA... We can't inherit constructors, so there is no need for them to be virtual. Comments Stefano Mtangoo: That nails it :) 0 Discussion Starter Stefano Mtangoo 441 6 Years Ago Look up C++ for dummies..good for basic in C++, it helped me a lot. Ha