通过继承重载 = 运算符
嘿我想了解如何在存在继承时重载运算符= 没有成功。 代码示例:
class Person
{
private:
char* m_name;
char* m_lastName;
.....
public:
virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Person.h"
Person& Person:: operator=(const Person& other)
{
if(this == &other)
return *this;
delete[] m_name;
delete[] m_lastName;
if (other.m_name!=NULL)
{
m_name = new char[strlen (other.m_name)+1];
strcpy(m_name,other.m_name);
}
if (other.m_lastName!=NULL)
{
m_lastName = new char[strlen (other.m_lastName)+1];
strcpy(m_lastName,other.m_lastName);
}
return (*this);
}
现在假设 Student 从 Person 继承,= 运算符应该如何实现 我认为它应该如下所示,请纠正我,因为我可能是错的:
#include "Person.h"
class Student : public Person
{
private:
char* m_collageName;
.....
public:
virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Student.h"
Person& Student:: operator=(const Person& other)
{
if(this == &other)
return *this;
Person::operator=(other);
delete[] m_collage;
if ((Student)other.m_collageName != NULL)
{
m_collageName = new char[strlen((Student)other.m_collageName)+1];
strcpy(m_collageName,(Student)other.m_collageName);
}
return (*this);
}
提前非常感谢,非常感谢。
hey i am trying to understand how to overload the operator= when there is an inheritance
with no Success.
code example:
class Person
{
private:
char* m_name;
char* m_lastName;
.....
public:
virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Person.h"
Person& Person:: operator=(const Person& other)
{
if(this == &other)
return *this;
delete[] m_name;
delete[] m_lastName;
if (other.m_name!=NULL)
{
m_name = new char[strlen (other.m_name)+1];
strcpy(m_name,other.m_name);
}
if (other.m_lastName!=NULL)
{
m_lastName = new char[strlen (other.m_lastName)+1];
strcpy(m_lastName,other.m_lastName);
}
return (*this);
}
now lets say Student inherit from Person how should the = operator should be implemented
i think it should be like the following please correct me cause i am probably being wrong:
#include "Person.h"
class Student : public Person
{
private:
char* m_collageName;
.....
public:
virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Student.h"
Person& Student:: operator=(const Person& other)
{
if(this == &other)
return *this;
Person::operator=(other);
delete[] m_collage;
if ((Student)other.m_collageName != NULL)
{
m_collageName = new char[strlen((Student)other.m_collageName)+1];
strcpy(m_collageName,(Student)other.m_collageName);
}
return (*this);
}
Thanks alot in advance much appriciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虚拟赋值运算符对我来说似乎是令人厌恶的。赋值适用于值类型,而
虚拟
适用于多态类型。这两者几乎处于光谱的两端。想象一下这段代码:
加上我们已经有了这个
,现在我们将其称为:
那应该做什么?把学生变成老师?为何如此?
A
virtual
assignment operator seems an abomination to me. Assignment is for value types, whilevirtual
is for polymorphic types. And these two are pretty much on opposite ends of the spectrum.Imagine, for a while, this code:
plus we're having this
and now we're calling this as:
What should that do? Turn a student into a teacher? How so?
你的实现不是类型安全的,因为我可以写:
它将成功编译分配,但会在运行时导致 UB (可能是段错误或垃圾读取),因为你向下转换了
operator=
的正确参数从Person
到Student
,虽然它不是一个。更一般地说,虚拟赋值运算符实际上没有意义,因为您必须在所有派生类中从
Person
(而不是特定子类)定义赋值,并且它不太可能对他们来说是一项有意义的手术。Your implementation is not typesafe, because I can write:
And it will compile the assignment successfully, but will result in U.B. (likely segfault or garbage read) at runtime, because you downcast the right argument of
operator=
fromPerson
toStudent
, while it is not one.More generally, a virtual assignment operator doesn't really make sense, since you'll have to define assignment from
Person
(rather than a specific subclass) in all derived classes, and it is very unlikely that it is a meaningful operation for them.