c++ - What does A a() mean? -
this question has answer here:
- most vexing parse 1 answer
consider code:
#include<iostream> using namespace std; class { public: a():age(12){} int age; }; int main() { a(); cout << a.age << endl; return 0; }
when compile using g++, error:
you can not see member age, because not class a()
can explain me? a a()
?
this line
a a();
declares function named a
, returning a
no arguments. (see most vexing parse).
what want is
a = a(); // value-initialization a{}; // same valid in c++11 (and not supported msvs)
or
a a; // default initialization
c++11, §8.5/10
note: since () not permitted syntax initializer,
x a();is not declaration of value-initialized object of class x, declaration of function taking no argument , returning x.
for class, value-initialization == default-initialization (at least outcome). see answer here: c++: initialization of int variables implicit constructor infos on value- vs. default-initialization pod or built-in types.
Comments
Post a Comment