Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour of operator= in C++

I have a base class A and two derived classes B and C. B defines the = operator, taking the base class A as parameter.

When calling = on class B, sometimes the operator of base class A is called instead of the one from B.

class A {
        public:

        void operator=(A &) {
                printf("A =\n");
        };
};

class B : public A {

        public:

        void operator=(A &s) {
                printf("B =\n");
        };
};

class C : public A {
};

int main()
{
        B b1, b2;
        C c;

        b1 = b2;
        b1 = c;
}

Output is:

A =
B =
  • Why is the first assignment not calling B::operator=()?

  • Why is the second assignment not calling A::operator=() as well, as it is also derived from A?

  • What can i do to make B::operator=() be called every time?

I was totally surprised when i saw this. I noticed it only because i deleted operator=() ("operator=() = delete") in class A, leading to a compiler error.

like image 308
nuppigeller Avatar asked Apr 18 '20 15:04

nuppigeller


People also ask

What are operators in C/C++?

Operators are the foundation of any programming language. Thus the functionality of C/C++ programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.

What is sizeof operator in C/C++?

sizeof operator: sizeof is a much used in the C/C++ programming language. It is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t. Basically, sizeof operator is used to compute the size of the variable.

What is an operator in programming?

We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands.

What is the addition operator in C++?

Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. C/C++ has many built-in operator types and they are classified as follows:


1 Answers

Your B::operator= is not a copy-assignment operator. In addition to the one you provide, there's also an implicitly-defined copy-assignment operator that is equivalent to

B& operator=(const B& other) {
  A::operator=(other);
  return *this;
}

This operator doesn't print anything, but it calls the assignment on the base class, and that one prints A=.

b1 = b2 calls this copy-assignment operator. b1 = c calls B::operator=(A&) since C is not a B.

If you want your operator to be called, define a copy-assignment operator with the signature shown above, instead of or in addition to the other overload.

like image 67
Igor Tandetnik Avatar answered Oct 12 '22 12:10

Igor Tandetnik