InsideDarkWeb.com

How to make this operator= overload work?

I was experimenting with making the operator= be virtual, like so:

#include <iostream>
#include <string>

class Base {
public:
    std::string field1;
    Base(std::string str) : field1(str) {}

    virtual Base& operator=(const Base& other)
    {
        field1 = other.field1;
        return *this;
    }
};

class Derived : public Base {
public:
    std::string field2;
    Derived(std::string str1, std::string str2) : Base(str1), field2(str2) {}

    virtual Derived& operator=(const Derived& other) override
    {
        Base::operator=(other);
        field2 = other.field2;
        return *this;
    }
};

However, this gives a compiler error, because the Derived function is not actually overloading anything, the signatures are different.

Is it possible to override the operator= to write code like this?

Base* ptr = new Derived("old 1", "old 2");
Derived derived("new 1", "new 2");

*ptr = derived; // <- use the derived class operator= to assign both field1 and field2

Stack Overflow Asked on November 15, 2021

1 Answers

One Answer

This operator

virtual Derived& operator=(const Derived& other);

does not override the copy assignment operator declared in the base class.

You have to write

Derived& operator=(const Base& other) override;

That is the type of the parameter shall be const Base &.

Here is a demonstrative program.

#include <iostream>

struct A
{
    virtual A & operator =( const A & )
    {
        std::cout << "A::operator =n";
        return *this;
    }
};

struct B : A
{
    virtual B & operator =( const A &a ) override
    {
        A::operator =( a );
        std::cout << "B::operator =n";
        return *this;
    }
};


int main() 
{
    B b1;
    
    A &rb1 = b1;
    
    B b2;
    b2 = rb1;
    
    return 0;
}

Its output is

A::operator =
B::operator =

Answered by Vlad from Moscow on November 15, 2021

Add your own answers!

Related Questions

If and excel formula for matching

1  Asked on November 23, 2020 by howaboutno

   

Pyspark Transpose

1  Asked on November 23, 2020 by sawan-s

     

How to aggregate data, pass and fail as final result?

1  Asked on November 23, 2020 by user584018

 

Comparing two lists for same value

1  Asked on November 22, 2020 by sebaku

     

Crystal Reports won’t display related table data

2  Asked on November 22, 2020 by rharris

   

Upgrading spring cloud version from Edgware to Hoxton

3  Asked on November 21, 2020 by ruchira-gayan-ranaweera

       

How to prevent button form Angular?

5  Asked on November 20, 2020 by user13976004

 

SQL union seems to fail for me

1  Asked on November 19, 2020 by sasha-peric

   

My Projectiles Stop Shooting Problem How To Fix?

1  Asked on November 19, 2020 by habib-ismail

   

Why Linq Distinct() does not work for my ConsultantList?

1  Asked on November 19, 2020 by karim-husein

         

Flutter app: simple way to remember a user to do something

1  Asked on November 19, 2020 by mactrix

 

Loop through a new json object every X seconds

2  Asked on November 19, 2020 by unluckylad

     

why css file was damaged on server

2  Asked on November 18, 2020 by hamid-talebi

   

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.