#include
#include
#include
class Integer
{
public:
int m;
Integer(int a):m(a){};
};
class CompareParts
Integer obj2()
isn't the definition of an object, it is the declaration of a function named obj2
returning an Integer
(put it outside any function to understand why it is so). This occurs also sometimes with more complex constructions where it can be even more confusing. Some name this the most vexing parse.
Here is the promised example of a more complex case:
struct Foo {};
struct Bar { Bar(Foo); };
Bar quxx(Foo()); // quxx is a function
Here quxx
is a function returning a Bar and taking (a pointer) to a function returning a Foo and without parameters. You could write the same declaration more clearly like this:
Bar quxx(Foo (*fn)()); // quxx is the same function as above
To get the definition of a variable initialized with the constructor taking a Foo, you can add a level of parenthesis:
Bar quux((Foo())); // quux is a variable