What does X f () mean?

I see this code, and I do not understand what it means. I know how we use the default constructor, but this is not a standard constructor. What is it?

class X
{
        ...
};

int main()
{
     X f();
}
+3
source share
4 answers

It declares a function fthat takes no parameters and returns a type X.
This is also known as Most Vexing Parse in C ++. This is a byproduct of how the C ++ standard defines the rules for interpreting declarations.

+7
source

Suppose you declared a function:

int Random();

And use it:

int main()
{
   int n;
   n = Random();
}

Random main. , Random . , Random - , .

, :

T foo();

, foo, T. T.

+3

f

  X          f();
  ^          ^ function   
  return type 

The function f()takes no arguments and returns a class object X.

for example, its definition may be as follows:

class X{
   int i;
   // other definition
}

X f(){ 
    X x;
    // some more code
    return x; 
}  

Basically you can use like:

int main(){

 X a = f();
 int i = f().i;
}
+2
source

This is a function that takes no arguments and returns an object of class X

+1
source

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


All Articles