Home > OS >  How to create an object in a form like this: ifstream in();
How to create an object in a form like this: ifstream in();

Time:04-10

Im beginner in c . I have seen several times when object created like:

class_name object_name();

and after that you can refer to object_name as an object of the class. How can i do this in my class? Should I override the constructor? And how to do that?

CodePudding user response:

This line of code can probably trigger a vexing (but not "the most vexing") parse behavior: instead of being interpreted as a variable declaration, it will be interpreted as the declaration of a function named object_name, taking no parameters and returning a value of type class_name.

See it happen on GodBolt: The compiler tells you:

<source>: In function 'void foo()':
<source>:4:27: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
    4 |     class_name object_name();
      |               
  • Related