Constructor Syntax

Having defined a class A with several constructors and a default constructor the statement A a( x ); calls the corresponding constructor, given it is defined, depending on the type of x. Hence you could guess that A a( ); should call the default constructor as it is defined with empty brackets A::A( ){}. But you must write A a; without brackets instead of A a(); even if the syntax without brackets doesnt fit smoothly into the general syntax of constuctor calls. Could somebody please give some insight why the syntax is defined this way? Thanks!
closed account (zb0S216C)
A a()

This declaration doesn't invoke the default constructor, but declares a prototype function, so the compiler thinks.

Wazzak
Last edited on
If something can be a function declaration it is a function declaration.
When you write A a(); the compiler will treat this as a function declaration of a function named a that takes no arguments and returns an A object.
Is a function declaration/prototype in a function body C++ compliant?
yes
Hence you could guess that A a( ); should call the default constructor
But that overlaps with a function declaration.
even if the syntax without brackets doesnt fit smoothly into the general syntax of constuctor calls
However for POD you wouldn't use parenthesis.
Thanks!
Last edited on
ne555 wrote:
However for POD you wouldn't use parenthesis.

Why not? When using dynamic memory allocations with POD types parenthesis even makes a difference.

Here *p is undefined
int* p = new int;

Here *p is initialized to 0
int* p = new int();
Last edited on
closed account (zb0S216C)
Users.Peter87.Respect++;

Wazzak
Topic archived. No new replies allowed.