Results 1 to 2 of 2

Thread: virtual constructor

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default virtual constructor

    Hi guys,

    1) Why don't we have virtual constructors?
    2) What is the main use of pure virtual destructor?

    Please explain giving some examples as I need to have visualization for this....

    Any comment will be highly appreciated...

    Thankx

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: virtual constructor

    Quote Originally Posted by vermarajeev View Post
    1) Why don't we have virtual constructors?
    What would we need it for? You need virtual methods only if you want to make sure that if you call that method, the implementation from the correct class is used, no matter what points to the object.

    Qt Code:
    1. struct A {
    2. void f1() { cout << "A::f1" << endl; }
    3. virtual void f2() { cout << "A::f2" << endl; }
    4. };
    5.  
    6. struct B : public A {
    7. void f1() { cout << "B::f1" << endl; }
    8. void f2() { cout << "B::f2" << endl; }
    9. };
    To copy to clipboard, switch view to plain text mode 

    Now if you do this:
    Qt Code:
    1. A *ptr = new B;
    2. ptr->f1();
    3. ptr->f2();
    To copy to clipboard, switch view to plain text mode 
    you'll receive:
    A::f1 (non-virtual, called according to the pointer)
    B::f2 (virtual, called according to the object class)

    It doesn't make much sense with constructors, as constructors are always called according to the class (you might say they are implicitely virtual), because you always specify what object you want to create ("new B" in the above mentioned example).

    2) What is the main use of pure virtual destructor?
    Virtual destructor is required to make sure that the correct destructor gets called if you have virtual methods.

    Pure virtual destructor (as every other pure virtual method) assures you can't make an instance of this class - you have to subclass and implement the method there and you'll be able to create instances of the subclass only.

Similar Threads

  1. link error for visual studio.net 2003
    By berlin in forum Newbie
    Replies: 9
    Last Post: 29th September 2006, 16:06
  2. Qt/Embedded Installation error during make
    By mahe2310 in forum Installation and Deployment
    Replies: 5
    Last Post: 7th September 2006, 04:05
  3. linking libraries
    By JustaStudent in forum Newbie
    Replies: 29
    Last Post: 2nd May 2006, 08:30
  4. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26
  5. use libs under qt4
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 27th February 2006, 17:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.