Thread: empty constructors?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    empty constructors?

    if i have a base class that has all of its functions declared virtual, even the constructor, then the classes that inherit from this class, how is the constructor called?

    for example
    Code:
    class blah {
    
    public:
     virtual blah() {};
     virtual void yay() {};
     virtual ~blah() {};
    }
    
    class woo : public blah {
    public:
     woo() { blah * hello = new blah(); } //what is this actually doing?
     void yay() {};
     ~woo() {};
    }

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Because you are dynamically allocating a new 'blah', the constructor for blah will be called.

    EDIT: Which, in this case, wont do anything, because the constructor is empty.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Uhh... Make destructors virtual, but not constructors. There is no need for the concept of virtual constructors (the correct constructor will always be called explicity. Derived classes should, however, call their base class constructor in the initializer list (however, this is done automatically in the case of constructors that thake no arguments.

    An example:
    Code:
    struct A {
       A(int x) { }
    };
    
    struct B : public A {
       B( ) : A(42) { }
    };
    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. skipping empty files using ifstream
    By bradleym83 in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2005, 07:15 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  5. testing constructors..
    By ronkane in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2002, 11:45 AM