English 中文(简体)
恢复至 o
原标题:Function returning to ostream

我想知道,是否有可能建立恢复一部分橄榄树的功能,例如:

#include <iostream>

class Point {
  public:
    Point(int x, int y){
      this->x = x;
      this->y = y;
    }

    ?? getXY(){  // I wish this function returned ostream
        return ??;
    }
  private:
    int x,y;
};

int main() {
  Point P(12,7);
  std::cout << "(x,y) = " << P.getXY(); // (12, 7);
}

我祝愿产出:

(x,y) = (12,7)  

我不想回到任何str或果阵列。 我可否在一定程度上回去一部分流?

问题回答

Generally this is done by overloading the stream insertion operator for your class, like this:

class Point {
  public:
    Point(int x, int y){
      this->x = x;
      this->y = y;
    }

    int getX() const {return x;}
    int getY() const {return y;}
  private:
    int x,y;
};

std::ostream& operator<<(std::ostream& out, const Point& p)
{
    out << "(x,y) =" << p.getX() << "," << p.getY();
    return out;
}

用于:

Point p;
cout << p;

为什么不只执行<条码>操作器和带;以及。 这正是你们想要的。

如果你只需要印刷一种产出,则只需要压倒operator<<。 但是,如果你需要根据不同情况印刷不同类型的产出,你可能会尝试制造不同代用类别的物体。

代理物体可参考<代码><>>>,并根据您的需要予以印刷(或部分)。

I would make the proxy objects private member classes of Point to restrict their visibility.

http://www.ohchr.org。 删除样本——我没有注意到这是家事。

除您的<代码><><>>>>>代码外,您还可使用助手功能(低于<条码>(<<>>> > >>)作为超载替代物:

std::ostream& display(std::ostream &os,Point &p) const {
 os<< p.x << p.y ;
 return os;
}

int main() {
    Point p;
    display(std::cout,p);    
        // This will call the display function and 
        // display the values of x and y on screen.
} //main

<代码><>>>> 代码/代码>功能,如果需要与私人成员联系,可打上<密码> 友好/代码>。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签