Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why must you provide the keyword const in operator overloads

Just curious on why a param has to be a const in operation overloading

CVector& CVector::operator= (const CVector& param)
{
  x=param.x;
  y=param.y;
  return *this;
}

couldn't you have easily done something like this ??

CVector& CVector::operator= (CVector& param) //no const
{
  x=param.x;
  y=param.y;
  return *this;
}

Isn't when something becomes a const, it is unchangeable for the remainder of the applications life ?? How does this differ in operation overloading ???

like image 400
numerical25 Avatar asked Jun 01 '10 13:06

numerical25


People also ask

What is const operator C++?

In C++ programming, when we implement the methods for classes, we usually add const specifier to make sure that the members of the class instance will not be modified by the method.

Which function is used to overload an operator?

Operators Overloading in C++ Thus, a programmer can use operators with user-defined types as well. Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

Which is used to overload the Greater Than operator?

C++ Program to overload the Greater than > operator In this program we try to overload the > operator with C++. Greater number C++ Program with operator overloading. n1 is greater than n2.

Can you have multiple operator overloads?

Yes. Show activity on this post.


1 Answers

You don't need const:

@numerical25: Just curious on why a param has to be a const in operation overloading

It's not required, but it is a good design decision.

See the C++ standard Section 12.8-9:

A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X&


I think it's a good idea though:

Using a const parameter does seems like a logical design decision to me though because you want to ensure that the other value will not be changed.

It tells other people that use your class that you will not be changing the other value when you say something like: myObject = other; and it enforces this so you can't accidentally change other.

Also if you allowed non const references to the object as the parameter, then you are limiting the amount of objects that can use your function. If it is const it can be used for parameters that are const and non const. If your parameter is non const it can only be used by parameters that are non const.


const only applies to the current reference, not the object:

@numerical25: Isn't when something becomes a const, it is unchangeable for the remainder of the applications life ?? How does this differ in operation overloading ???

A const reference is simply that a reference that is const. It does not change the const-ness of the actual object you are passing in.


An example of non-const operator overloading:

Here is an example of operator overloading where the parameter is not const.
I DO NOT RECOMMEND TO DO THIS THOUGH:

class B
{
public: 
 const B& operator=(B& other)
 {
  other.x = 3;
  x = other.x;
  return *this;
 }

 int x;
};


void main(int argc, char** argv[])
{
 B a;
 a.x = 33;
 B b;
 b.x = 44;
 a = b;//both a and b will be changed
 return 0;
}
like image 150
Brian R. Bondy Avatar answered Oct 02 '22 16:10

Brian R. Bondy