" + QUOT;运算符在C ++中重载 [英] "+" operator overloading in C++
问题描述
#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;
}
问题:
在编写运算符+功能时,我的意图是添加几个类的成员但不是全部。我们通常编写operator +函数的方式将覆盖所有类成员,因为我们在operator +()中创建了一个本地对象。我们可以避免这个吗?在这种情况下如何保留其他班级成员的价值?
我尝试过:
试图了解用法。可能这是它的工作原理。需要确认。
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.
推荐答案
参见二进制算术运算符部分/ w / cpp / language / operators>运算符重载 - cppreference.com [ ^ ]指南。
See the "Binary arithmetic operators" section of operator overloading - cppreference.com[^] for a guideline.
编写二进制算术运算符有三个规则:
1.您还必须写一个复合算子,+ =在你的情况下。
2.你的复合算子必须返回一个参考
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.使用复合赋值运算符定义二进制算术运算符:
< br $>
3. Define your binary arithmetic operators using your compound assignment operators:
const MyClass MyClass::operator+(const MyClass &other) const {
return MyClass(*this) += other;
}
听起来你正在尝试将各种对象合并为一个:一个具有直观可理解的求和概念,以及另一个没有。他们仍然以某种方式相关,你希望将它们放在一个共同的容器中。
我建议将你的'添加数据'与像这样的'用户数据':
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.
这篇关于" + QUOT;运算符在C ++中重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!