中使用Const变量进行重载

中使用Const变量进行重载

本文介绍了运算符=在C ++中使用Const变量进行重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你们是否可以帮助我.

I was wondering if you guys could help me.

这是我的.h:

Class Doctor {
   const string name;
   public:
       Doctor();
       Doctor(string name);
       Doctor & Doctor::operator=(const Doctor &doc);
}

和我的主要人:

int main(){
    Doctor d1 = Doctor("peter");
    Doctor d2 = Doctor();
    d2 = d1;
}

我想执行operator =函数.谁能帮我?注意Doctor上的const成员.

I want to do the operator= function. Can anyone help me? Notice the const member on Doctor.

*********************我的主要问题是我希望另一个类具有像医生那样的医生属性.但我希望能够更换我的医生.就像我正在看医生A但我想看医生B一样.这将在我的另一个班级(Pacient)中使用setDoctor函数来完成.如果是我在做代码,我会说这样的话:

*********************My main problem is that I want another class to have an attribute which is a Doctor like a Pacient has a Doctor. But I want to be able to change my Doctor. Like i am seeing doctor A but I want to see Doctor B. That would be done using a setDoctor function in my other class (Pacient). If it was me doing the code I would say something like this:

class Patient{
    Doctor &d;
};

,然后更改指针.但是,我使用的是一位教师制作的基本代码,它的类定义如下:

and then change the pointer. However I am using a base code made by one of the teachers and it has the class defined like:

class Patient{
     Doctor d;
}

但是我认为这是不可能的,因为在Patient类中使用setDoctor()时,我要么创建副本,要么更改变量本身.第一个没有什么区别,第二个由于const是不可能的.我说的对吗?

But I think this is impossible to do because with a setDoctor() in the Patient class I would either make a copy or alter the varable itself. The first doesn't make any difference and the second is impossible due to the const. Am I right?

推荐答案

您快到了.值得注意的几点:

You are almost there. Few noteworthy points:

  • 该名称不应为const限定. const不能被修改,这正是我们在赋值运算符中想要的.

  • The name should not be const qualified. A const cannot be modified, which is exactly what we want in the assignment operator.

C ++关键字是class,而不是Class,因为您的代码具有它(它将给您带来编译错误)

The C++ keyword is class and not Class as your code has it (it'll give you compile errors)

正如迈克尔·伯尔(Michael Burr)所指出的:但应注意,如果该类仅包含已经正确支持赋值的其他类(如本例中的简单字符串成员),则隐式的,由编译器生成的operator = ()可以正常工作."在这里,根据您的情况,唯一的成员string具有适当的op=.因此,明确定义是多余的.

As Michael Burr notes: "It should be noted though that if the class simply contains other classes that already properly support assignment (as in this case with a simple string member), the implicit, compiler-generated operator=() will work just fine." Here, in your case, the only member string has a proper op=. So explicitly defining is redundant.

Meeh的解决方案就在那里.它唯一不谈论的是自我分配.阅读常见问题解答12 .

Meeh's solution is almost there. The only thing it doesn't talk about is self-assignment. Read FAQ 12.

分配是三大成员函数之一常见问题解答27.10 .查一下它说,要求实现复制ctor,op =或dtor之一通常意味着您也需要实现另外两个.

Assignment is one the Big Three member functions FAQ 27.10. Look it up. It says, requirement to implement either one of copy ctor, op= or the dtor usually implies that you'd need to implement the other two as well.

更正后的代码示例应如下所示:

The corrected code sample should be something like this:

class Doctor {
  string name;
  public:
    Doctor& operator=(Doctor const& o) {
         if (&o != this) name = o.name;
         return *this;
    }
  // ...
};

这篇关于运算符=在C ++中使用Const变量进行重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:41