Click here to Skip to main content
14,699,500 members
Please Sign up or sign in to vote.
4.11/5 (3 votes)
See more:
#include <iostream>

using namespace std;

class Test {
private:
	int mx;
	float my;
	int medata; //some extra data

public:
	Test() { mx = my = medata = 0; }
	Test(int x, float y, int edata) { mx = x; my = y; medata = edata; }

	void print() { cout << "x = " << mx << " y = " << my << " edata = " << medata << endl; }

	void modify_edata(int data) { medata = data; }

	Test operator+ (const Test& other)
	{
		Test sum;
		sum.mx = this->mx + other.mx;
		sum.my = this->my + other.my;
		return sum;
	}
	};

int main()
{
	Test t1(10, 50.5, 0), t2(20, 60.5, 0);
	Test t3;

	t3.modify_edata(1000);
	t3.print();

	t3 = t1 + t2;

	t3.print();

	return 0;
}
Question:
When writing operator "+" functionality, my intention would be to add few members of class but not all. The way we generally write "operator+" function will overwrite all the class members because we create a local object in "operator+ ()". Can we avoid this? How to retain the values of other class members in this case?

What I have tried:

Trying to understand the usage. May be this is how it works. Need confirmation.
Posted
Updated 28-Mar-19 23:58pm
v2

See the "Binary arithmetic operators" section of operator overloading - cppreference.com[^] for a guideline.
   
Comments
Member 13508014 28-Mar-19 7:40am
   
I read "Binary arithmetic operators" section of operator overloading - cppreference.com[^].
When we write a = b + c, according to the article, here 'b' will also get altered isn't it.
CPallini 28-Mar-19 7:49am
   
Nope, a copy of b whill be altered.
Maciej Los 29-Mar-19 13:23pm
   
5ed!
CPallini 30-Mar-19 6:59am
   
Thank you, Maciej.
Member 13508014 31-Mar-19 3:41am
   
Yes. Copy of b will be altered. I agree that.

But, I think, my question is not understood here.

My question is, in "a = b + c", lets assume that the object "a" is created a bit long and its state is modified now and when i call "a = b + c", i expect the addition operation to happen on only few members and those will get altered. Other member of object "a" for which "+" operation does not make sense should be same as before calling "a = b + c".
CPallini 31-Mar-19 5:55am
   
Possibly I actually misunderstood your original question.
If you are asking if 't3.edata' erase in
t3 = t1 + t2;
is correct, then yes, it is correct: it is so by the assignment operator semantics (t3 should be exactly the sum of t1 and t2).
Member 13508014 1-Apr-19 13:50pm
   
Yes. I too realized and agree that the point that t3 should be exactly the sum of t1 and t2. Thanks CPallini for your time in answering my question.
CPallini 1-Apr-19 15:50pm
   
You are welcome.
There are three rules for writing binary arithmetic operator:
1. You also must write a compound operator, += in your case.
2. Your compound operator must return a reference

MyClass & MyClass::operator+=(const MyClass &rhs) {
    ...
    return *this;
  }


3. Define your binary arithmetic operators using your compound assignment operators:

const MyClass MyClass::operator+(const MyClass &other) const {
    return MyClass(*this) += other;
  }
   
v2
Comments
Stefan_Lang 29-Mar-19 4:41am
   
Good advice. But the most important part of it should be pointed out separately: that operator+ should be declared as a const member function! (the OP forgot to do that)
Maciej Los 29-Mar-19 13:23pm
   
5ed!
It sounds like you're trying to merge to kinds of objects into one: one that has an intuitively understandable concept of summation, and another that doesn't. Still they are related somehow to the point that you want to have them inside a common ... container.

I'd suggest to separate your 'additive data' from the 'user data' like this:

class MyValues {
  int mx;
  float my;
public:
  MyValues(int x, float y) : mx(x), my(y) {}
  MyValues(const MyValues& other) : mx(other.mx), my(other.my) {}
  MyValues& operator +=(const MyValues& other) {
    mx += other.mx;
    my += other.my;
    return *this;
  }
  MyValues operator+(const MyValues& other) const {
    return MyValues(*this)+=other;
  }
};
class MyData {
  int mdata;
public:
  MyData(int data) : mdata(data) {}
  MyData(const MyData& other) : mdata(other.mdata) {}
  void set(int data) { mdata = data; }
  int data const ( return mdata; }
};
class MyClass {
  MyValues mvalues;
  MyData mdata;
public:
  MyClass(int x, float y, int data) : mvalues(x, y), mdata(data) {}
  void addValues(const MyClass& other) {
    mvalues += other.mvalues;
  }
};

This allows you to separate user data functionality from numerical data functionality.
   
Comments
Maciej Los 29-Mar-19 13:23pm
   
5ed!

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