why virtual constructor

Discussion in 'C++' started by Devika, Oct 4, 2005.

  1. Devika

    Devika Guest

    hi,

    I understand the need of virtual destructor.but virtual constructor is
    not supported in c++.but basically why we need to have virtual
    constructor??

    thx in advance

    Devika
     
    Devika, Oct 4, 2005
    #1
    1. Advertising

  2. Devika

    mlimber Guest

    mlimber, Oct 4, 2005
    #2
    1. Advertising

  3. Devika wrote:
    >
    > hi,
    >
    > I understand the need of virtual destructor.but virtual constructor is
    > not supported in c++.but basically why we need to have virtual
    > constructor??


    Because sometimes you have a pointer to an object. You don't know
    which object exactly, but you do know that it is somehow derived
    from some baseclass. And now you want a copy of that object. How
    would you do that.

    eg.
    You have geometric shapes (aka a graphical editor). Now you
    want to implement cut&paste operation. For this you need to be able
    to generate copies of the selected shapes. But all you have are a bunch
    of shape pointers, but you don't know if these pointers point to
    circles, lines, splines, areas or texts.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 5, 2005
    #3
  4. Devika

    Devika Guest

    thx i got it...

    Devika
     
    Devika, Nov 2, 2005
    #4
  5. Devika

    Kaz Kylheku Guest

    Devika wrote:
    > hi,
    >
    > I understand the need of virtual destructor.but virtual constructor is
    > not supported in c++.but basically why we need to have virtual
    > constructor??


    It makes no sense for a C++ constructor to be virtual, because the
    exact type of an object is statically obvious at compile time. Based on
    the declared, manifest type, the compiler knows exactly what
    constructors have to be called and in what order.

    A virtual destructor is needed so you can destroy an object through a
    base class pointer. You invoke ``delete b;'' on a ``Base *b'' which
    might actually be pointer to a Derived. The compiled code has to
    properly destroy that object regardless of its type, and the virtual
    destructor mechanism does that.

    There is sometimes a need to construct an object whose type is not
    known. For example, suppose you have some program that lets the user to
    enter a class name as a string, like "wizard" or "warrior". You parse
    the string, and based on what is parsed, you have to construct
    different types of objects.

    There are a number of ways to do that kind of thing. One way is the
    Abstract Factory pattern. You use the run-time data (string or
    whatever) as a key to fetch a factory from dictionary of factories.
    Then you call the virtual functions on the factory to make objects of
    that factory's type for you.

    You may also need a mechanism to deal with construction parameters. The
    factory interface may have to take some kind of abstract list of
    keyword-value pairs. Each implementation of that interface will have to
    parse from that structure the things it knows about, perform the
    appropriate conversions, and then invoke the constructor with the right
    arguments. Or perhaps internally choose from among several different
    overloads of the constructor.
     
    Kaz Kylheku, Nov 2, 2005
    #5
  6. Devika

    Devika Guest

    thx a lot for the detailed explanation..it helped a lot

    Devika
     
    Devika, Nov 3, 2005
    #6
    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. Giulio
    Replies:
    9
    Views:
    1,047
    Patrick Kowalzick
    Jun 25, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    910
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,999
    Smokey Grindel
    Dec 2, 2006
  4. Devika

    why virtual constructor

    Devika, Oct 4, 2005, in forum: C++
    Replies:
    0
    Views:
    318
    Devika
    Oct 4, 2005
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,246
Loading...

Share This Page