嗯,我想知道爲什麼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