重载运算符 << 可以打印出两个不同的函数吗?
Overloading operator << Possible to print out two different functions?
这是我的一小段代码:
Cord::Cord(int x, int y){
x_ = x;
y_ = y;
}
Cord::Cord(int x, int y, int z){
x_ = x;
y_ = y;
z_ = z;
}
std::ostream& operator <<(std::ostream& out, Cord& x) {
out << x.x_ << " " << x.y_ << " " << x.z_;
return out;
}
对于我的两个函数重载,是否可以使运算符在 << 函数上重载。现在,如果我只使用 x 和 y 的函数,它也会打印出 z。有没有办法让 << 运算符打印出两个函数而不打印出 z 当我只有 x 和 y 或者这是不可能的?
您需要 z_ 的默认值。
Cord::Cord(int x, int y){
x_ = x;
y_ = y;
z_ = 0; // If 0 is a good default value for z_
}
使用默认值,解决方案之一可能是
std::ostream& operator <<(std::ostream& out, Cord& x) {
if(z_!= 0) // If 0 is your default value for z
out << x.x_ << " " << x.y_ << " " << x.z_;
else
out << x.x_ << " " << x.y_;
return out;
}
请注意,您的代码设计不当。
更新:设计命题
了解 Encapsulation
和 Polymorphism
部分解决方案:
Coord2d.cpp
Coord2d::Coord2d(int x, int y){
x_ = x;
y_ = y;
}
int Coord2d::getX(){
return x_;
}
int Coord2d::getY(){
return y_;
}
std::ostream& operator <<(std::ostream& out, Coord2d& coords2d) {
out << x.x_ << " " << x.y_;
}
Coord3d.cpp
Coord3d::Coord3d(int x, int y, int z){
x_ = x;
y_ = y;
z_ = z;
}
int Coord3d::getX(){
return x_;
}
int Coord3d::getY(){
return y_;
}
int Coord3d::getZ(){
return z_;
}
std::ostream& operator <<(std::ostream& out, Coord3d& coords3d) {
out << x.x_ << " " << x.y_ << " " << x.z_;
}
使用 optional
,您可以:
class Coord
{
public:
Coord(int x, int y) : x(x), y(y) {}
Coord(int x, int y, int z) : x(x), y(y), z(z) {}
friend std::ostream& operator <<(std::ostream& out, const Coord& c)
{
out << c.x << " " << c.y;
if (c.z) { out << " " << *c.z; }
return out;
}
private:
int x;
int y;
boost::optional<int> z;
};
这是我的一小段代码:
Cord::Cord(int x, int y){
x_ = x;
y_ = y;
}
Cord::Cord(int x, int y, int z){
x_ = x;
y_ = y;
z_ = z;
}
std::ostream& operator <<(std::ostream& out, Cord& x) {
out << x.x_ << " " << x.y_ << " " << x.z_;
return out;
}
对于我的两个函数重载,是否可以使运算符在 << 函数上重载。现在,如果我只使用 x 和 y 的函数,它也会打印出 z。有没有办法让 << 运算符打印出两个函数而不打印出 z 当我只有 x 和 y 或者这是不可能的?
您需要 z_ 的默认值。
Cord::Cord(int x, int y){
x_ = x;
y_ = y;
z_ = 0; // If 0 is a good default value for z_
}
使用默认值,解决方案之一可能是
std::ostream& operator <<(std::ostream& out, Cord& x) {
if(z_!= 0) // If 0 is your default value for z
out << x.x_ << " " << x.y_ << " " << x.z_;
else
out << x.x_ << " " << x.y_;
return out;
}
请注意,您的代码设计不当。
更新:设计命题
了解 Encapsulation 和 Polymorphism
部分解决方案:
Coord2d.cpp
Coord2d::Coord2d(int x, int y){
x_ = x;
y_ = y;
}
int Coord2d::getX(){
return x_;
}
int Coord2d::getY(){
return y_;
}
std::ostream& operator <<(std::ostream& out, Coord2d& coords2d) {
out << x.x_ << " " << x.y_;
}
Coord3d.cpp
Coord3d::Coord3d(int x, int y, int z){
x_ = x;
y_ = y;
z_ = z;
}
int Coord3d::getX(){
return x_;
}
int Coord3d::getY(){
return y_;
}
int Coord3d::getZ(){
return z_;
}
std::ostream& operator <<(std::ostream& out, Coord3d& coords3d) {
out << x.x_ << " " << x.y_ << " " << x.z_;
}
使用 optional
,您可以:
class Coord
{
public:
Coord(int x, int y) : x(x), y(y) {}
Coord(int x, int y, int z) : x(x), y(y), z(z) {}
friend std::ostream& operator <<(std::ostream& out, const Coord& c)
{
out << c.x << " " << c.y;
if (c.z) { out << " " << *c.z; }
return out;
}
private:
int x;
int y;
boost::optional<int> z;
};