Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody,

I have a question for you about overloading operator = for derived classes.
Suppose for example to have two classes A, B defined like this:

class A
{
protected:
	double v_;

public:

	A(double initVal = 0.0)
	: v_(initVal)
	{}

	virtual ~A() {}
	
	double v() 
        {
	   return v_;
	}

	void v(double val) { v_ = val; }


	A& operator=(A& src)
	{
	   this->v(src.v());
	}

};


//defining sum 
A operator+(A& add1, A& add2)
{
	A res;
	res.v(add1.v() + add2.v());
	return res;
}


class B : public A
{
public:

	B(double initVal = 0.0)
		: A(initVal)
	{}

	virtual ~B() {}

	B(B& src)
		: A(src.v())
	{}
	
	B(A& src)
		: A(src.v())
	{}


	B& operator=(B& src)
	{
		B res;
		res.v(src.v());
		return res;
	}

	B& operator=(A& src)
	{	
		B res;
		res.v(src.v());
		return res;
	}
};


now if i debug the following lines:

A a(1.0);
B b(2.0);	
B c;
//first Assignemnt
c = a;
//second assignment
c = a + b;


i can see that the both the assignment pass through

B& B::operator=(A& src)


Now if i modify

B& operator=(B& src)


to

B& operator=(B src)


the second assignment (with the sum) passes through this operator.

Somebody can please explain why this happens?

thank you

What I have tried:

Is the reason of this behaviour the implicit cast?
Posted
Updated 5-Dec-17 3:58am

Make the constructor
B(A& src)
explicit.
Smth like
explicit B(A& src)
 
Share this answer
 
Comments
Optimistic76 5-Dec-17 8:10am    
Hi tra_la_la_la,

ok if i make explicit the constructor both the assignment pass through B::operator=(A&) but i would like to know what is the logic behind this.
Why the A a is considered is some way differente from the object that the addition operator returns?
I have not checked your code and what it does because it is not implementing the assignment operators in the usual way. They should not return a copy but the object itself (note also that the operator in your class A is returning nothing).

It should be:
class A
{
    // ...
    double v() const
    {
        return v_;
    }
    
    A& operator=(const A& src)
    {
        this->v(src.v());
        return *this;
    }

    // This is missing in your code and should be there
    friend A operator+(const A& add1, const A& add2);
};

A operator+(const A& add1, const A& add2)
{
    // EDIT: Or just (because we are friend, we have direct access to the members)
    return A(add1.v_ + add2.v_);
    //A res;
    //res.v(add1.v() + add2.v());
    //return res;
}

class B
{
    // ...
    B& operator=(const B& src)
    {
        this->v(src.v());
        return *this;
    }
     
    B& operator=(const A& src)
    {	
        this->v(src.v());
        return *this;
    }
};
Note also that the parameters should be const and so should be the getter function v().
 
Share this answer
 
v3
I suggest you to have a look at this page: Copy constructors, assignment operators, - C++ Articles[^].
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900