What does A a() mean? [duplicate]

This line

A a();

declares a function named a, returning A with no arguments. (See Most vexing parse).

What you want is

A a = A(); // value-initialization
A a{}; // the same but only valid in C++11 (and currently not supported by MSVS)

or

A a; // default initialization

C++11, ยง8.5/10

Note: Since () is not permitted by the syntax for initializer,

X a();

is not the declaration of a value-initialized object of class X, but the declaration of a function taking no argument and returning an X.

For your class, value-initialization == default-initialization (at least for the outcome).
See my answer here: C++: initialization of int variables by an implicit constructor for Infos on value- vs. default-initialization for POD or built-in types.

Leave a Comment