Jump to content
  • Advertisement
Sign in to follow this  
discman1028

= operator overloading, and Inhertiance

This topic is 4401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For the following code, I get this output: A::operator= 1 2 0 4 Can you tell me if my logic is correct? The "dummy1 = dummy2;" statment invokes the implicitly defined B::operator=, which copies B::d, then A::operator= is called..? Can someone explain where the "0" came from?
#include <iostream>
using namespace std;


class A
{
public:
	A() : a(0), b(0), c(0) {}
	int a;
	int b;
	int c;

	A& operator= (A& rhs)
	{
		a = rhs.a;
		b = rhs.b;
		cout << "A::operator=()" << endl;
		return *this;
	}
};

class B : public A
{
public:
	B() : d(0) {}

	int d;
};

int main()
{
	B dummy1;
	B dummy2;

	dummy2.a = 1;
	dummy2.b = 2;
	dummy2.c = 3;
	dummy2.d = 4;

	dummy1 = dummy2;

	cout << dummy1.a << endl;
	cout << dummy1.b << endl;
	cout << dummy1.c << endl;
	cout << dummy1.d << endl;



	return 0;
}




Share this post


Link to post
Share on other sites
Advertisement

A& operator= (A& rhs)
{
a = rhs.a;
b = rhs.b;
cout << "A::operator=()" << endl;
return *this;
}


you forgot to set c to rhs.c.


Share this post


Link to post
Share on other sites
I did that on purpose, as a test case. I wanted to see if the derived B::operator= would copy *all* members. But apparently A::operator= is still called.

I read that if you defined your own B::operator=, then A::operator= is not called. So, if I wanted added functionality in B::operator=, but still wanted A::operator= to be called for an object, I would have to invoke A::operator=() explicitly within B::operator= ?

Just trying to figure out the details of operator inheritance...

Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by discman1028
if I wanted added functionality in B::operator=, but still wanted A::operator= to be called for an object, I would have to invoke A::operator=() explicitly within B::operator= ?

yes

Share this post


Link to post
Share on other sites
The zero clearly comes from dummy1 itself. You don't replace 'c' with a new value, so it keeps the value it had originally. In this case, it happens to be 0.

Bs assignment operator isn't going to mess with As members...that's what As assignment operator is there for.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by discman1028
I did that on purpose, as a test case. I wanted to see if the derived B::operator= would copy *all* members. But apparently A::operator= is still called.


Copying a B obviously involves copying the A sub-object. Like every other member of B, if it has its own copy operator, that will get called.

Quote:
I read that if you defined your own B::operator=, then A::operator= is not called. So, if I wanted added functionality in B::operator=, but still wanted A::operator= to be called for an object, I would have to invoke A::operator=() explicitly within B::operator= ?


Correct.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!