Thread: virtual

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    Cool virtual

    can any body please say me about virtual constructor and destructor

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    "me about virtual constructor and destructor"

    No, really, what do you want to know ? I'm not going to write a tutorial for you but I can help you on a specific question though.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Constructors can't be virtual.

    Destructors of a base class should almost always be virtual to ensure that the proper destructor is called. Eg.
    Code:
    Base* pBase = new Derived;
    ...
    delete pBase;
    In the code above, if the destructor in Base is virtual, then the delete would call Derived::~Derived(); but if the destructor was not virtual, it would call Base::~Base(). Since pBase is actually a pointer to Derived, the wrong destructor would be called and bad things could happen.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cpjust View Post
    Constructors can't be virtual.
    This is true, but there is an idiom known as the "virtual copy constructor". In this code;
    Code:
    class Base
    {
        public:
    
            virtual ~X() {};    // virtual destructor
    
            virtual Base *Clone() const = 0;
    };
    
    class Derived: public Base
    {
        public:
    
             virtual Base *Clone() const {return new Derived(*this);};
    }
    
    int main()
    {
        Base *x, *y;
        x = new Derived;
        y = x->Clone();
        //   x and y both point at an instance of Derived, and *y is a copy of *x
    }
    the Clone() member function is referred to as a "virtual copy constructor". It must be supplied by every class derived from Base, and is used to create a copy of an object (assuming that both base and derived classes have properly functioning copy constructors).

  5. #5
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    a *virtual* constructor is not really a constructor, it's a member function that returns a pointer to the class it's declared in.

    virtual constructor is really an oxymoron, they don't really exist. though its purpose may seem to be a constructor, it's just a member function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM