Is there a difference between `List x;` and `List x ()`

The name comes from the famous C ++ FAQ site from Marshall Cline.

The author claims that there is a difference between the two code examples.

Suppose List is the name of some class. The f () function then declares a local List object named x:

void f() { List x; // Local object named x (of class List) ... } 

But the g () function declares a function called x (), which returns a List:

 void g() { List x(); // Function named x (that returns a List) ... } 

But is it really wrong to use the second option?

And if this is really a declaration, the compiler would not have granted that you cannot declare a function inside a function?

+4
source share
1 answer

And if this is really an declaration, the compiler would not have granted that you cannot declare a function inside a function.

Of course not. Because it can declare a function with a function.

This is called the most unpleasant analysis , and it is well documented. In fact, it would be a mistake on behalf of the compiler to consider

 List x(); 

as a variable declaration.

But is it really wrong to use the second option?

If you want a variable, then yes. If you want to declare a function ... kinda yes. You can, but usually you do it outside the scope.

+6
source

Source: https://habr.com/ru/post/1432680/


All Articles