Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
M

Mattias Hogstrom

@Mattias Hogstrom
About
Posts
5
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • why no virtual constructor?
    M Mattias Hogstrom

    jschell wrote:

    It is defined behavior.

    I suppose you mean that the defined behavior is that it isn´t allowed. I meant that its behavior would be undefined if it was allowed.

    jschell wrote:

    Not sure what that means. It not possible because the language definition doesn't allow it. Perhaps your reason is a restatement of why that rule exists.

    Yes.

    C / C++ / MFC c++ question

  • why no virtual constructor?
    M Mattias Hogstrom

    you're right. I remember reading that before. It is unsafe or undefined to call a virtual method from the constructor. I was mearly trying to show the usage of virtual methods that can create objects by delegating the exact type to a subclass to define it. This is how some factories are implemented. But I should have stopped there. I went too far. Calling them from constructors are of course not possible due to how parents an children are created. I have never found any need for a virtual constructor, but virtual methods that create objects often.

    C / C++ / MFC c++ question

  • why no virtual constructor?
    M Mattias Hogstrom

    I didn't try to answer your question :) The original question was "why no virtual constructor" (I suppose he is asking because there are virtual destructors) You are confusing me.... talking about valid answers. There is probably a reason why there is no virtual constructor in C++ If the language designer would have wanted one, it could have been added regardless if it was useful or not.

    C / C++ / MFC c++ question

  • why no virtual constructor?
    M Mattias Hogstrom

    Yes the default behavior is that all constructors execute, the subclass and all its parents constructors, so initialization isn't a problem. Overriding is generally done by declaring methods virtual. I thought he meant how to "override" the constructor, but that doesn't make much sense. That was my point I was trying to get to. You were asking on the other hand asking: What would a virtual constructor create? But what is actually common is to have virtual methods that create subclass objects. This is how the factory pattern works. The parent class provides a CreateInstance Method. specialized factory implementations(subclasses), create related objects of the same family. An SqlServerConnection object provides a CreateCommand method that returns a SqlServerCommand, whereas the OracleConnection object's CreateCommand would return an OracleCommand. The Create methods are not class constructors, but they are constructing objects. It is probably not safe to call virtual methods from a constructor, because the parent constructors execute before the constructors of the subclasses have finished.

    C / C++ / MFC c++ question

  • why no virtual constructor?
    M Mattias Hogstrom

    Consider the opposite, that it was possible. Subclassing, can sometimes be viewed as specialization. Dog is a specializations of an Animal. If the Animal has private variables, these are initialized in the constructor. If the constructor is virtual, it is replaced by a subclass' constructor. According to the principles of data hiding (public, protected, private), a sub class cannot access private member variables of a parent type. So these member variables would be left uninitialized. You don't want that because the parent class would break. If you by some reason need a virtual constructor there is a Design Pattern called the Template Method, http://en.wikipedia.org/wiki/Template\_method\_pattern In the example they have they define an algorithm in the parent class, and let the subclass define the implementation. It is of course possible to apply the same principle in constructors by creating a virtual method called "Initialize" which you call from the constructor. When this method is redefined in a subclass it will override the parent's definition. But I have never came across the need for a virtual constructor. Why do you need one?

    C / C++ / MFC c++ question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups