Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

overloaded '=' operator in c++ class

I am looking at some existing C++ code and I see several c++ classes that are derived from abstract base classes.

I see that all these classed have an overleaded '=' operator that is public.
SomeClass& operator = (const SomeClass& rhs);

Open in new window

Can someone explain why this is needed??
And why is it public??

4 Solutions
käµfm³d 👽Commented:
This is similar to a copy constructor. These methods of "copying" (including copy constructor) are used to ensure that you have a deep copy vs. a shallow copy.

You declare this method public because it will be accessed outside of the class itself. If you only ever performed copies internally (i.e. within the class), then I believe you could declare it private.
evilrixSenior Software Engineer (Avast)Commented:
Just like constructors, classes don't inherit the public access of their base class assignment operators, so they have to define their own.

As kaufmed noted, in general these would be public unless you are specifically preventing others from copying your class.
to add to above:

you actually don't need neither copy constructor nor operator= for your class if all of the class members (types) can be copied or assigned safely. the compiler automatically would create proper copy constructor and operator= for you if needed (means, if you create a copy of a class object somewhere in your code). for example if you have a  class with int members and std::string members only, the int members were copied bitwise and for the std::string the copy constructor and operator= of that class was used. not providing a copy constructor and operator= in cases where it is possible makes your code less error-prone and would automatically actualize if you add more members.

the above doesn't work for member pointers cause pointers normally should not be copied by pointer value but by copying the object they were pointing to. so if your class has pointer members or arrays of pointers you always need to implement appropriate copy constructor and operator=.

note, the return type Someclass& of operator= function would allow statements like

Someclass a, b, c;
a = geta();
c = b = a;

Open in new window

The simplest answer for providing an overloaded "=" operator is so that you can treat your class object like an integer, enabling your code to be more readable and more easily convey your intent.
For example
int a = 1; int b = 2
int answer = a + b  // simple integer division
class Person aa("Fred")
class Person bb("Mary")
class Person result;

result = aa // simple assignment (at least in looks), underneath there could be very complex work being done by a copy constructor, but it looks nice and simple on your main code page.

It is made public so that it can be accessed.

Just as an aside, if you do use the assignment operator, you have to decide whether you mean this to be a deep copy or a shallow copy of the object in question.
A shallow copy essentially means you are creating a separate copy of the object, with its own address space. A deep copy means you are creating a copy of the object, but some or all of the underlying memory addresses will be the same in both objects, ie, some members will point to the same allocated memory.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now