Regarding Virtual Constructor

Discussion in 'C++' started by ramesh, May 22, 2006.

  1. ramesh

    ramesh Guest

    Why do we have a virtual destructor and why cant we have a virtual
    constructor?
     
    ramesh, May 22, 2006
    #1
    1. Advertising

  2. ramesh

    Sumit Rajan Guest

    Sumit Rajan, May 22, 2006
    #2
    1. Advertising

  3. ramesh wrote:
    > Why do we have a virtual destructor and why cant we have a virtual
    > constructor?


    Until the object is fully constructed (and its final type known), how
    would you decide which constructor function to invoke to construct it?

    There is a "virtual constructor" idiom, look it up, but it's not a C++
    concept, although it's used in C++.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 22, 2006
    #3
  4. ramesh

    Sunil Varma Guest

    ramesh wrote:
    > Why do we have a virtual destructor and why cant we have a virtual
    > constructor?


    An object is constructed on calling the constructor.
    Virtual functions are associated to objects.
    A constructor is called before the object is constructed.
    So you cannot associate a constructor after the object is created.
     
    Sunil Varma, May 22, 2006
    #4
  5. ramesh

    Rolf Magnus Guest

    Victor Bazarov wrote:

    > ramesh wrote:
    >> Why do we have a virtual destructor and why cant we have a virtual
    >> constructor?

    >
    > Until the object is fully constructed (and its final type known), how
    > would you decide which constructor function to invoke to construct it?


    Well, the final type is always known at compile time when constructing an
    object. That's why a virtual constructor makes no sense.
     
    Rolf Magnus, May 22, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dotnetprogram
    Replies:
    1
    Views:
    727
    Joey Powell
    Dec 27, 2003
  2. Replies:
    1
    Views:
    324
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Feb 6, 2007
  3. Replies:
    4
    Views:
    287
    Steve Holden
    Sep 27, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,288
  5. somenath
    Replies:
    22
    Views:
    347
    Öö Tiib
    Oct 25, 2013
Loading...

Share This Page