Calling Default Constructor

This is a discussion on Calling Default Constructor within the C++ Programming forums, part of the General Programming Boards category; If I create my own class, MyClass and then declare it my code as follows: Code: MyClass mc; Is it ...

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

    Calling Default Constructor

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

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

    Or should I declare the following to be safe:

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

    BigAngryDog.com

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    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.

    gg

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21