好吧,我想知道为什么endd似乎没有执行(尽管它在编译时不会产生任何错误). struct dxfDato{ dxfDato(int c, string v = 0, int t = 0) { codigo = c; valor = v; tipo = t; } dxfDato() { } int tipo; int codigo; string valor
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
谢谢你们!
您已经将类型操作定义为通过引用获取std :: ostream并通过引用返回std :: ostream的函数,但是您已定义endd以获取dxfItem并返回dxfItem并且dxfItem不是从std派生的:: ostream的.由于这种类型不匹配,编译器正在生成对运算符的调用<<模板,而不是操纵过载.
此外,你的操作重载需要实际调用传递给它的操纵器函数:
dxfItem& operator<<(manip x)
{
x(ss);
return *this;
}
