jeudi 10 mai 2018

Declaring object of a class in global namespace vs. declaring it in a class [duplicate]

This question already has an answer here:

Consider the following snippet:

class Foo
{
public:
    Foo(int a, std::string b){}
};

Foo a(1, "a"); //object created in global namespace
Foo b(int, std::string); //function declaration

class Bar
{
    Foo c(1, "a"); //invalid function declaration
};

In global scope, we can create an object of class Foo. However, in the class definition the same line is interpreted as ill-formed function declaration, returning object Foo.

My guess was that C++11 standard should allow such initialization, together with Foo c = Foo(1, "a");, but I was wrong.

My question is: Is there particular reason for standard to not support such object initialization in class declaration? Outside of class, it can clearly distinguish between object initialization and function declaration.

Aucun commentaire:

Enregistrer un commentaire