C++ C++;相当于Java';什么是toString?
对于自定义类的对象,我想控制写入流的内容,即C++ C++;相当于Java';什么是toString?,c++,C++,对于自定义类的对象,我想控制写入流的内容,即cout。C++中有可能吗?在java中,可以重写类似C++的代码> toString()/Case>方法。C++中的< P>可以将运算符> p>作为约翰所说的扩展,如果要提取字符串表示并将其存储在 STD::String 中,请执行以下操作:< /P> #include <sstream> // ... // Suppose a class A A a; std::stringstream sstream; sstream <
cout
。C++中有可能吗?在java中,可以重写类似C++的代码> toString()/Case>方法。C++中的< P>可以将<代码>运算符> p>作为约翰所说的扩展,如果要提取字符串表示并将其存储在<代码> STD::String 中,请执行以下操作:< /P>
#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
#包括
// ...
//假设是a类
A A;
std::stringstream和ssstream;
sstream>s,但这将跳过空白
std::stringstream
位于
标题中。您也可以这样做,允许多态性:
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); }
类基{
公众:
虚拟std::ostream&dump(std::ostream&o)常量{
在C++11中返回o,to_字符串最终添加到标准中
问题已经得到了回答。但我想补充一个具体的例子
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;
}
类点{
公众:
点(intthex,intthex):x(theX),y(thes)
{}
//打印对象
friend ostream&operator最好先声明operatorBetter,然后将其声明为friend
,也可以在类的主体内部-这样,您就不必对包含运算符(和类)的命名空间使用,但只要该类的对象是操作数之一,ADL就会找到它……上面的意思是“将其定义为类主体中的朋友”-就像在中一样,一个内联成员定义。@fnieto:thatdump
public方法是肮脏的和不必要的。在这里使用friend
是非常好的。您是喜欢一个冗余的方法还是一个侵入性的friend
完全是一个品味问题,尽管friend
可能就是为了这个目的而引入的。@Pavel:只要操作符与类定义在同一名称空间中,依赖参数的查找就会找到它。这与朋友无关,也不需要在类内声明/定义它。另外,为虚拟函数设置operator+1,复制Java的toString
行为。为什么不直接声明/定义它呢指定运算符因为您不希望有无限循环和崩溃。此技术可以快速、轻松地传递有关序列化内容的选项。否则,需要定义另一个类友好运算符。另一点是,转储功能的实现可以由接口强制执行,该接口不可能使用建议的运算符。这是对这个页面的有用补充,但是C++实现与爪哇/C语言中的一个实现有很大的不同。是定义在所有对象基类上的虚函数,因此被用作表示任何对象的字符串表示的标准方式。这些函数在 STD::String 中只适用于内置类型。C++中的惯用方式是覆盖<代码>“丑陋”。运算符的标准签名这是获取序列化字符串的一种荒谬而繁琐的方法!
#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
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); }
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;
}