2つのコンストラクターを持つクラスがあります。1つは引数を取らず、もう1つは引数を取ります。
1つの引数を取るコンストラクターを使用してオブジェクトを作成すると、期待どおりに機能します。ただし、引数を取らないコンストラクタを使用してオブジェクトを作成すると、エラーが発生します。
たとえば、このコードをコンパイルすると(g ++ 4.0.1を使用)...
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2();
foo2.bar();
return 0;
}
...次のエラーが発生します。
nonclass.cpp: In function ‘int main(int, const char**)’:
nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’
これはなぜですか、どのように機能させるのですか?