【发布时间】:2009-05-19 10:28:09
【问题描述】:
如果我编译(在 G++ 下)并运行以下代码,它会打印“Foo::Foo(int)”。然而,在将复制构造函数和赋值运算符设为私有后,它无法编译并出现以下错误:“错误:‘Foo::Foo(const Foo&)’是私有的”。如果它只在运行时调用标准构造函数,它怎么需要一个复制构造函数?
#include <iostream>
using namespace std;
struct Foo {
Foo(int x) {
cout << __PRETTY_FUNCTION__ << endl;
}
Foo(const Foo& f) {
cout << __PRETTY_FUNCTION__ << endl;
}
Foo& operator=(const Foo& f) {
cout << __PRETTY_FUNCTION__ << endl;
return *this;
}
};
int main() {
Foo f = Foo(3);
}
【问题讨论】:
标签: c++ constructor