virtual constructors
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: virtual constructors

  1. #1
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    virtual constructors

    I guess last time i asked a wrong question.....i should have asked

    why constructors can't be virtual??
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: virtual constructors

    A constructor cannot be virtual, because to create an object, we need complete information of its type. "virtual constructor" is the name of a technique for calling a virtual function to create an object of an appropriate type.

    Furthermore, have a look at the followings
    [20.8] What is a "virtual constructor"?
    Why don't we have virtual constructors?

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: virtual constructors

    Quote Originally Posted by sunnypalsingh
    why constructors can't be virtual??
    In simple words: Virtual functions allow a function implementation to be chosen dynamically, depending on the object's class. Before you construct an object, the object does not exist. A non-existing object logically cannot have a class.

  4. #4
    Join Date
    Sep 2005
    Posts
    1

    Re: virtual constructors

    Why don't we have virtual constructors?

    I see this question from 2 angles.
    1) Is it syntactically possible to write a virtual constructor?
    2) If yes, then what purpose it would serve?

    Most of the answers posted were for second question. However, even if I decide to write a virtual constructor, (I'm not bothered about the result) I'm still not able to do so.
    (One of ) The reason for this is - virtual functions needs a object OR a explicit call.
    The difference between a constructor and a destructor is you can explicitly call a destructor on an object, but you cannot explicitly call a constructor. (Though this would again boil down to same logic - if the object is already constructed why do I need to explicitly call a constructor? So the object must be constructed.) This also explains that the virtual effect for an object starts after the completion of constructor.

    I actually wanted to test this with placement constructors too, but surprizingly MSVC++6.0 doesn't support this syntax and throws error saying :- 'new' : function does not take 2 parameters

    Following was the piece of code for placement constructor

    #include <iostream.h>

    class Base
    {
    public:
    char BaseData[10];

    Base()
    {
    cout<<"In Base()"<<endl;
    f();
    }

    virtual void f()
    {
    cout<<"In Base::f()"<<endl;
    }

    };

    class Derived : public Base
    {
    public:
    char DerivedData[10];

    Derived()
    {
    cout<<"In Derived()"<<endl;
    f();
    }

    virtual void f()
    {
    cout<<"In Derived::f()"<<endl;
    }
    };


    int main()
    {
    char a[255], b[255];
    char c[255];

    Base *pBase;
    Derived *pDerived;

    pBase = new (a) Base;
    pBase = new (b) Derived;
    pDerived = new (c) Derived;

    return 0;
    }

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: virtual constructors

    How would you implement a virtual constructor if you could have one? How would you perceive it working.

    Code:
    class Base
    {
    public:
      virtual Base() // if such a thing were allowed
      { 
        std::cout << "construct a virtual base" <<std::endl;
      }
      virtual ~Base() {}
      virtual int f() { return 0; }
    };
    
    class Derived1 : public Base
    {
    public:
       virtual Derived1()
       {
         std::cout << "constructing virtual Derived1" << std::endl;
       }
       virtual int f()
       {
            return 1;
       }
    };
    
    class Derived2 : public Base
    {
    public:
       virtual Derived2()
       {
         std::cout << "constructing virtual Derived2" << std::endl;
       }
       virtual int f()
       {
            return 2;
       }
    };
    Now, suppose this were allowed. How would you call the virtual constructor of Base to construct a Derived1 or a Derived2?

    By the way, you can have virtual inheritance, which would mean that Base is constructed only by the final class.

  6. #6
    Join Date
    Sep 2004
    Posts
    561

    Re: virtual constructors

    Constructors cannot be virtual because it doesn't make any sense.
    Objects are constructed from the base down to the lowest derived class in the inheritance hierarchy. If your constructor is virtual then you would be using a derived class constructor before a base was even created.
    What's the purpose of using a derived class if the base hasn't been created yet? You'll run into a crash if your derived class tries to use a base class data member.

  7. #7
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: virtual constructors

    One way u can answer your question, is by asking yourself...

    Why do I need a virtual constrctor ???
    What purpose will it serve ???
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471

    Re: virtual constructors

    Rigel has it right: the concept simply doesn't make sense. It's like asking why a constructor can't be static - the two concepts don't overlap.

    In some ways it's a pity that things like clone functions and factory functions get called "virtual constructors". The term's fine when you understand the basic incompatibility between (real) ctors and the virtual call mechanism, but only confuses the issue if you don't.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center