Thread: default constructor == constructor without argument?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    default constructor == constructor without argument?

    If an object is instantiated without brackets, is it the same as having empty brackets?

    Code:
    class A { ... };
    A a;
    Code:
    class A { ... };
    A a();
    Are they the same?

    The only way I can get different "behaviours" out of them is

    Code:
    class A {
    	A() {}
    };
    
    int main() {
    	A a();
    }
    vs
    Code:
    class A {
    	A() {}
    };
    
    int main() {
    	A a;
    }
    Note that the constructor is private. Both work if it's declared public.

    The first one compiles fine, but the second one gives
    cyberfish@cyberfish-desktop:/tmp$ g++ a.cpp
    a.cpp: In function ‘int main()’:
    a.cpp:2: error: ‘A::A()’ is private
    a.cpp:6: error: within this context
    Anyone care to enlighten me?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The first version doesn't declare an object of type A. It declares a function which returns an object of type A.

    Try this and see:
    Code:
    class A {
        A() {}
        public:
        void foo() {}
    };
    
    int main()
    {
        A a();
        a.foo();
    }

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Uh...

    Nice call!

    Too much Java for me recently.

    Thanks for pointing it out.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What if it's allocated on the heap?

    Code:
    class A {
        public:
        A() {}
        void foo() {}
    };
    
    int main()
    {
        A *a = new A();
        a->foo();
    }
    Code:
    class A {
        public:
        A() {}
        void foo() {}
    };
    
    int main()
    {
        A *a = new A;
        a->foo();
    }
    They both compile. Are they the same?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    For objects it is the same.
    For built-in types however, using () zero-initialises the variable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I see. Thanks.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Using () would zero-initialize any POD-struct as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error declaring default argument to friend function
    By Sebastiani in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2009, 01:52 AM
  2. Pointer to List Iterator As Function Argument
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2009, 05:30 AM
  3. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  4. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 06:12 PM