Assignment Operator in C++ can be created virtual. Why do needed? Are we able to make other operators virtual too?

It is dependent around the operator.

The purpose of making a project operator virtual is to permit you from the advantage of having the ability to override it copying more fields.

If you come with an Base&lifier and also you really possess a Derived&lifier like a dynamic type, and also the Derived has more fields, the right situations are replicated.

However, there's a risk that the LHS is really a Derived, and also the RHS is really a Base, then when the virtual operator runs in Derived your parameter isn't a Derived and you've got no way to get fields from it.

This is a good discussio: http://icu-project.org/paperwork/papers/cpp_report/the_assignment_operator_revisited.html

A job operator isn't needed to become made virtual.

The discussion below is all about operator=, it is applicable to the operator over-loading that can take within the key in question, and then any function that can take within the key in question.

The below discussion implies that the virtual keyword doesn't learn about a parameter's inheritance when it comes to getting a matching function signature. Within the final example it shows how you can correctly handle assignment when confronted with inherited types.


Virtual functions haven’t heard of parameter's inheritance:

A function's signature must be exactly the same for virtual to come up. So know within the following example, operator= is created virtual. The phone call won't ever behave as an online function in D since the parameters and return worth of operator= will vary.

The function B::operator=(const B&lifier right) and D::operator=(const D&lifier right) are 100% different and viewed as 2 distinct functions.

class B

amplifier operator=(const B&lifier right)

  

  int x



class D : public B

amplifier operator=(const D&lifier right)

  

  int y




Default values and getting 2 overloaded operators:

You are able to though define an online function to let you set default values for D when it's designated to variable of type B. This really is even when your B variable can be a D saved right into a reference of the B. You won't obtain the D::operator=(const D&lifier right) function.

Within the below situation, a project from 2 D objects saved inside 2 B references... the D::operator=(const B&lifier right) override can be used.

//Use same B as above

class D : public B

amplifier operator=(const D&lifier right)

  

  virtual B&lifier operator=(const B&lifier right)

  

  int y



int primary(int argc, char **argv)

b2

  printf("d2.x d2.y %i %in", d2.x, d2.y)

  return



Prints:

d1.x d1.y 99 100

d2.x d2.y 99 13

Which implies that D::operator=(const D&lifier right) isn't used.

With no virtual keyword on B::operator=(const B&lifier right) you'd have a similar results as above but the need for y wouldn't be initialized. I.e. it might make use of the B::operator=(const B&lifier right)


One further key to tie it altogether, RTTI:

You should use RTTI to correctly handle virtual functions that consume your type. This is actually the last bit of the puzzle to learn how to correctly handle assignment when confronted with possibly inherited types.

  virtual B&lifier operator=(const B&lifier right)

  

    else

    

    return *this