玉家伙在这里,我的问题很简单..我想构建不同势值类型getter和setter ..基本上函数重载但getter和setter ..我试图像这样getter和setter C++
#include <iostream>;
class Vectors {
public:
Vectors() {};
Vectors(int a, int b) {
x = a, y = b;
}
int getX() {
return x;
}
int getY() {
return y;
}
float getX() {
return (float)x;
}
float getY() {
return (float) y;
}
friend Vectors operator+(const Vectors& v1, const Vectors& v2);
friend Vectors operator/(const Vectors& v1, const Vectors& v2);
protected:
int x, y;
private:
};
Vectors operator+(const Vectors& v1, const Vectors& v2) {
Vectors brandNew;
brandNew.x = v1.x + v2.x;
brandNew.y = v1.y + v2.y;
return (brandNew);
};
Vectors operator/(const Vectors& v1, const Vectors& v2) {
Vectors brandNew(v1.x/v2.x, v1.y/v2.y);
return brandNew;
}
int main() {
Vectors v1(2, 3);
Vectors v2(4, 5);
Vectors v3;
v3 = v1 + v2;
Vectors v4 = v1/v2;
std::cout << "VECTOR 4 X : " << v4.getX() << std::endl;
std::cout << "VECTOR 4 Y : " << v4.getY() << std::endl;
std::cout << "Vector V3 X : " << v3.getX() << std::endl;
std::cout << "VECTOR V3 Y : " << v3.getX() << std::endl;
}
但显然它说不能做功能重载和唯一类型不同是返回类型。
所以问题是?为什么不在你称之为吸气剂之后进行剧组? –
问题是你怎么做它告诉我的论据必须是不同的... @BaummitAugen。以上代码有错误... – amanuel2
你不能,根据你的最后一句话是显而易见的。想一想,如果你编写'v4.getX()',编译器应该怎么知道你想调用哪个getter?再次,只需要'int' getters并且输出它们的结果。 –