overloaded '=' operator in c++ class

Posted on 2011-10-15
Last Modified: 2012-06-27
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??

Question by:Wanting2LearnMan
    LVL 74

    Accepted Solution

    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.
    LVL 39

    Assisted Solution

    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.
    LVL 32

    Assisted Solution

    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

    LVL 9

    Assisted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now