【发布时间】:2011-02-03 09:21:44
【问题描述】:
这是一个代码sn-p:
class test {
public:
test(){
cout<<"I am in default constructor ";
}
static void func(const test &obj){
cout<<"I am in function ";
}
protected:
test( const test &o){
cout<<"I am in copy constructor ";
}
};
int main()
{
test::func(test());
}
上面的代码在编译时使用 g++ 3.4.6(在 Red Hat Linux 上)出现以下错误:
在函数`int main()'中:
错误:`test::test(const test&)' 受保护
错误:在此上下文中
但是,如果您使用 g++ 3.3.2 或 g++ 4.4.3 或任何其他 g++ 版本(在 Red Hat Linux 上)编译,它会成功编译并给出以下输出:
我在默认构造函数中我在函数中
在上面的代码中,我通过引用将临时对象(由默认构造函数创建)传递给函数 func。那么为什么编译器 3.4.6 会调用拷贝构造函数呢?
【问题讨论】: