c++ c++-faq copy-constructor assignment-operator rule-of-three
如果是复制构造函数,private那么
情况1:没有错误,编译器不关心是否在类中定义了复制构造函数.
情况2:错误,复制构造函数是私有的,当它被创建时public,它被省略.
它是否直接优化了副本而没有注意到构造函数是否已经构建private?
#include <string>
using std::string;
class T
{
string s;
T(const T &obj):s(obj.s){}
public:
T(const string &str):s(str){}
};
int main()
{
T a = ("Copy Initialization"); //Case: 1
T b = T("Copy Initialization"); //Case: 2
}
Run Code Online (Sandbox Code Playgroud)