嗯,我想知道为什么endd
似乎并没有执行(虽然它不会在编译时产生任何错误)。C++ Manipulator not being
struct dxfDato
{
dxfDato(int c, string v = 0, int t = 0) { codigo = c; valor = v; tipo = t; }
dxfDato() { }
int tipo;
int codigo;
string valor;
};
class dxfItem
{
private:
std::ostringstream ss;
typedef std::ostream& (*manip)(std::ostream&);
public:
int clase;
string valor;
vector<dxfDato> datos;
vector<dxfItem> hijos;
template <typename T>
dxfItem& operator<<(const T& x)
{
ss << x;
return *this;
}
dxfItem& operator<<(manip x) // to store std manipulators
{
ss << x;
return *this;
}
static dxfItem& endd(dxfItem& i) // specific manipulator 'endd'
{
dxfDato dd;
dd.valor = i.ss.str();
i.datos.push_back(dd);
std::cout << "endd found!" << endl;
return i;
}
};
/* blah blah blah */
dxfItem header;
header
<< 9 << endl << "$ACADVER" << endl << 1 << endl << "AC1500" << endl
<< dxfItem::endd // this apparently doesn't execute anything
<< "other data" << endl
;
这是我在尝试开发某些东西时发现的最后一个问题。最后一件事暴露在这里:C++ Operator overloading example
谢谢大家!
绝对如此。无论如何,谈论你的答案的第二部分:为什么使用'ss << std :: x;'仍然不会调用操纵器'std :: x(ss);'? – huff 2010-02-04 15:31:16
@huff,其实你是对的。如果你正在为自己的类型创建一个操纵器(比如dxfItem),你需要使用函数调用语法,但是由于std :: ostringstream有自己的操作符<<操作符的重载,所以你应该可以使用操作符。 – 2010-02-04 15:37:29