Thread: Calling Default Constructor

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002

    Calling Default Constructor

    If I create my own class, MyClass and then declare it my code as follows:

    MyClass mc;
    Is it guaranteed to call its default constructor?

    Or should I declare the following to be safe:

    MyClass mc();
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Yes, it will call the constructor with no arguments or the one will all default arguments.

    >>MyClass mc();
    This declares a function "mc" that returns MyClass and takes void.


  3. #3
    Registered User
    Join Date
    Jun 2004
    C++ supports both styles. This because this version:

    MyClass mc();

    can also be a function prototype. Sometimes the compiler cant distinguish between a default construction and a function prototype and gives an error.

    Consider this:

    class A

    A a();

    int main()

    Is a a global object or a function declaration returning A and taking no params? For this case you can do this.

    A a;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling an object's constructor?
    By TriKri in forum C++ Programming
    Replies: 36
    Last Post: 08-18-2008, 01:03 PM
  2. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  3. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  4. Default Constructors w/Classes as Parameters
    By GrNxxDaY in forum C++ Programming
    Replies: 22
    Last Post: 07-31-2002, 07:50 AM
  5. no appropriate default constructor
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:12 AM