Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator = Overload with Const Variable in C++

I was wondering if you guys could help me.

Here are my .h:

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

and my main:

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

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

************EDIT:********* 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;
}

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?

like image 239
Inês Avatar asked Apr 12 '09 22:04

Inês


People also ask

What is const in operator overloading?

Overloading on the basis of const type can be useful when a function returns a reference or pointer. We can make one function const, that returns a const reference or const pointer, and another non-const function, that returns a non-const reference or pointer.

How do I overload operator+?

Overloading the plus operator (+) is as simple as declaring a function named operator+, giving it two parameters of the type of the operands we want to add, picking an appropriate return type, and then writing the function.

Can ++ overload?

The postfix increment operator ++ can be overloaded for a class type by declaring a nonmember function operator operator++() with two arguments, the first having class type and the second having type int . Alternatively, you can declare a member function operator operator++() with one argument having type int .

What is operator overloading with example?

This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.


2 Answers

You are almost there. Few noteworthy points:

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

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

  • 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's solution is almost there. The only thing it doesn't talk about is self-assignment. Read FAQ 12.

  • 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;
    }
  // ...
};
like image 135
dirkgently Avatar answered Oct 13 '22 11:10

dirkgently


The standard way to define the assignment constructor correctly so that it is exception safe is to define it in terms of the copy constructor.

class Doctor
{
    public:
        Doctor& operator=(Doctor const& rhs)
        {
            if (this != &rhs)
            {
                Doctor  tmp(rhs);  // Use copy constructor here
                this->swap(tmp);   // Now Swap
            }
            return *this;
        }
        void swap(Doctor& rhs) throws()
        {
            std::swap(.....); // swap each member variable.
        }
};

By doing it like this makes it exception safe.
Note you just need to make the swap a no-throw method, this is relatively simple if you are using STL objects as they all define a no-throw swap just for this situation as does boost and all good libraries (so you should follow suite).

If this going wrong they will go wrong in using the copy constructor. At this point you have not modified your own object as you are copy constructing into a temporary. Thus you are providing good exception safety as your object is still un-changed.

like image 36
Martin York Avatar answered Oct 13 '22 12:10

Martin York