例如:
class Derived : public Base
{
Derived(const Base &rhs)
{
// Is this a copy constructor?
}
const Derived &operator=(const Base &rhs)
{
// Is this a copy assignment operator?
}
};发布于 2013-02-17 07:03:05
显示的构造函数是否算作复制构造函数?
不是的。它将而不是算作复制构造函数。
它只是一个转换构造函数,而不是一个复制构造函数。
C++03标准复制类对象 Para 2:
类
X的非模板构造函数是一个复制构造函数,如果它的第一个参数是X&、const X&、volatile X&或const volatile X&类型,并且没有其他参数,或者所有其他参数都有默认参数。所显示的赋值运算符是否算作副本赋值运算符?
不,不是的。
C++03标准12.8复制类对象第9段:
用户声明的复制赋值操作符
X::operator=是类X的非静态非模板成员函数,其类型为X、X&、const X&、volatile X&或const volatile X&。
在线样本:
#include<iostream>
class Base{};
class Derived : public Base
{
public:
Derived(){}
Derived(const Base &rhs)
{
std::cout<<"\n In conversion constructor";
}
const Derived &operator=(const Base &rhs)
{
std::cout<<"\n In operator=";
return *this;
}
};
void doSomething(Derived obj)
{
std::cout<<"\n In doSomething";
}
int main()
{
Base obj1;
doSomething(obj1);
Derived obj2;
obj2 = obj1;
return 0;
}输出:
In conversion constructor
In doSomething
In operator=https://stackoverflow.com/questions/14918764
复制相似问题