对象的赋值操作符
Assignment Operator for an object
我写了一个代码,用于动态分配一个名称。我知道在这种情况下我应该处理深度拷贝。我所写的是我自己版本的复制构造函数、复制赋值操作符和析构函数。我应该重新定义任何其他隐式函数,如移动赋值操作符吗?我不清楚移动赋值操作符或任何其他隐式定义的成员函数的概念(除了我已经提到的)。谁能添加这个dynName code
的代码,以显示Move赋值运算符或任何其他隐式成员函数(如果有的话)
#include <iostream>
using namespace std;
class dynName{
char* name;
int size;
public:
dynName(char* name="")
{
int n=strlen(name)+1;
this->name= new char[n];
strncpy(this->name,name,n);
size=n;
name[size-1]=' ';//NULL terminated
cout << "Object created (Constructor) with name : "
<< name << " at address " << &(this->name) << endl;
}
dynName(const dynName& Ob)//Copy Constructor
{
int n=Ob.size;
this->name= new char[n];
strncpy(this->name,Ob.name,n);
size=n;
cout << "Object created(Copy constructor) with name : "
<< this->name << " at address " << &(this->name) << endl;
}
//Assignment Operator
dynName& operator=(const dynName& ob);
~dynName()
{
cout << "Object with name " << this->name << " at address " <<
&(this->name)<<" destroyed" << endl;
delete[] name;
name=0; //Avoiding Dangling pointer if any
}
//friend ostream& operator << (ostream& os,const dynName ob);
//Will Call Copy Constructor
friend ostream& operator << (ostream& os,const dynName& ob);
};
dynName& dynName::operator=(const dynName& ob)
{
// check for self-assignment
if (this == &ob)
cout << "Created with assignment Operator " << endl;
return *this;
// first we need to deallocate any value that this string is holding!
delete[] this->name;
this->size = ob.size;
// this->name = new char[this->size];
strncpy(this->name, ob.name,this->size);
cout << "Created with assignment Operator " << endl;
return *this;
}
//ostream& operator << (ostream& os,const dynName ob)
ostream& operator << (ostream& os,const dynName& ob)
{
os << "The name ("<< ob.size << " Letters) : " << ob.name << endl;
return os;
}
int main()
{
dynName Ob1("Andrew Thomas");
dynName Ob2;
dynName Ob3(Ob1);
dynName Ob4;
Ob4=Ob1;//Should Call Assignment Operator
cout << "nnn";
cout << Ob1;
cout << Ob2;
cout << Ob3;
cout << Ob4;
cout << "nnn";
return 0;
}
这段代码的问题是没有调用我的复制赋值操作符。有帮助吗,为什么?
。美元/试验
Object created (Constructor) with name : Andrew Thomas at address 0x22ac40
Object created (Constructor) with name : at address 0x22ac30
Object created(Copy constructor) with name : Andrew Thomas at address 0x22ac20
Object created (Constructor) with name : at address 0x22ac10
The name (14 Letters) : Andrew Thomas
The name (1 Letters) :
The name (14 Letters) : Andrew Thomas
The name (1 Letters) :
Object with name at address 0x22ac10 destroyed
Object with name Andrew Thomas at address 0x22ac20 destroyed
Object with name at address 0x22ac30 destroyed
Object with name Andrew Thomas at address 0x22ac40 destroyed
感谢编辑
引用Move赋值操作符和' if (this != &rhs) 'Class&&
是什么?我的意思是我从来没用过这种东西。只是引用,比如Class&
似乎你在这里缺少大括号:
if (this == &ob)
cout << "Created with assignment Operator " << endl;
return *this;
只有输出是if
主体的一部分,return
语句将始终执行。
应该调用复制操作符,但总是在自赋值检查之后返回。
dynName& dynName::operator=(const dynName& ob)
{
// check for self-assignment
if (this == &ob)
cout << "Created with assignment Operator " << endl;
return *this; //THIS LINE is not in the if block
// first we need to deallocate any value that this string is holding!
delete[] this->name;
this->size = ob.size;
// this->name = new char[this->size];
strncpy(this->name, ob.name,this->size);
cout << "Created with assignment Operator " << endl;
return *this;
}
相关文章:
- 在C++中,当表达式涉及对象时,将表达式赋值到对象中时,是否有定义的操作顺序?
- 重载运算符 -- 无法在赋值中将对象转换为基类型
- 赋值类对象的值
- 简单的引用变量赋值导致对象的全局指针出现segfault
- c++中的重载赋值操作符
- c++类继承和赋值操作符
- 带引用类成员的赋值操作符
- 复制构造函数,赋值操作符重载
- c++派生类赋值操作符
- 通过调用Move赋值操作符实现Move构造函数
- 带有映射的赋值操作符
- 单参数构造函数和赋值操作符
- c++模板赋值操作符重载——赋基本类型给对象
- 对象的赋值操作符
- 清除std::vector对象需要赋值操作符.为什么
- c++赋值操作符,如何将一个对象设置为等于一个新对象的实例
- 对象上的赋值操作符
- 声明对对象的引用和赋值操作符
- 退出重载赋值操作符将调用析构函数来删除对象
- c++:如果类禁用赋值操作符,是否有可能创建一系列类对象(使用vector?)