instantiating an object by calling it's default constructor such as with foo() seems to do something different than instantiating without the parentheses. can anyone tell me what the difference is? the program below illustrates what I'm talking about.
class foo {
public:
// constructors
foo( void ) : a(0) { };
explicit foo( constint x ) : a(x) { };
foo( const foo& other ) { a = other.a; }
foo& operator=( const foo& other ) { a = other.a; return *this; }
int a;
};
int main() {
foo bar;
foo b;
foo c(1);
foo d();
bar=b; // works
bar=c; // works
bar=d; // doesn't work
return 0;
}
compiler complains:
1 2 3 4 5
main.cpp: In function ‘int main()’:
main.cpp:19:9: error: no match for ‘operator=’ in ‘bar = d’
main.cpp:19:9: note: candidate is:
main.cpp:7:7: note: foo& foo::operator=(const foo&)
main.cpp:7:7: note: no known conversion for argument 1 from ‘foo()’ to ‘const foo&’
C++'s Most Vexing Parse. Really. That's what it's called.
Line 15 doesn't define an object named c of type foo constructed by calling its default constructor. It declares a function named c that takes no parameters and returns a foo.