C++ 相当于 Java 的 toString?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1549930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ equivalent of Java's toString?
提问by Bogdan Balan
I'd like to control what is written to a stream, i.e. cout
, for an object of a custom class. Is that possible in C++? In Java you could override the toString()
method for similar purpose.
我想控制写入流的内容,即cout
自定义类的对象。这在 C++ 中可能吗?在 Java 中,您可以toString()
出于类似目的覆盖该方法。
回答by sth
In C++ you can overload operator<<
for ostream
and your custom class:
在 C++ 中,您可以operator<<
为ostream
自定义类重载:
class A {
public:
int i;
};
std::ostream& operator<<(std::ostream &strm, const A &a) {
return strm << "A(" << a.i << ")";
}
This way you can output instances of your class on streams:
这样你就可以在流上输出你的类的实例:
A x = ...;
std::cout << x << std::endl;
In case your operator<<
wants to print out internals of class A
and really needs access to its private and protected members you could also declare it as a friend function:
如果您operator<<
想打印出类的内部结构A
并且确实需要访问其私有成员和受保护成员,您还可以将其声明为友元函数:
class A {
private:
friend std::ostream& operator<<(std::ostream&, const A&);
int j;
};
std::ostream& operator<<(std::ostream &strm, const A &a) {
return strm << "A(" << a.j << ")";
}
回答by fnieto - Fernando Nieto
You can also do it this way, allowing polymorphism:
你也可以这样做,允许多态:
class Base {
public:
virtual std::ostream& dump(std::ostream& o) const {
return o << "Base: " << b << "; ";
}
private:
int b;
};
class Derived : public Base {
public:
virtual std::ostream& dump(std::ostream& o) const {
return o << "Derived: " << d << "; ";
}
private:
int d;
}
std::ostream& operator<<(std::ostream& o, const Base& b) { return b.dump(o); }
回答by Zhaojun Zhang
In C++11, to_string is finally added to the standard.
在 C++11 中,to_string 最终被添加到标准中。
http://en.cppreference.com/w/cpp/string/basic_string/to_string
http://en.cppreference.com/w/cpp/string/basic_string/to_string
回答by blwy10
As an extension to what John said, if you want to extract the string representation and store it in a std::string
do this:
作为 John 所说的扩展,如果您想提取字符串表示并将其存储在 a 中,std::string
请执行以下操作:
#include <sstream>
// ...
// Suppose a class A
A a;
std::stringstream sstream;
sstream << a;
std::string s = sstream.str(); // or you could use sstream >> s but that would skip out whitespace
std::stringstream
is located in the <sstream>
header.
std::stringstream
位于<sstream>
标题中。
回答by blwy10
The question has been answered. But I wanted to add a concrete example.
问题已得到解答。但我想添加一个具体的例子。
class Point{
public:
Point(int theX, int theY) :x(theX), y(theY)
{}
// Print the object
friend ostream& operator <<(ostream& outputStream, const Point& p);
private:
int x;
int y;
};
ostream& operator <<(ostream& outputStream, const Point& p){
int posX = p.x;
int posY = p.y;
outputStream << "x="<<posX<<","<<"y="<<posY;
return outputStream;
}
This example requires understanding operator overload.
这个例子需要理解运算符重载。