C ++操作符重载示例 [英] C++ Operator overloading example
问题描述
好吧,我是操作员重载的新,我发现这个问题。而不是记录自己,我更喜欢问你:D
Well, I'm new to operator overloading, and I found this problem. Instead of documenting myself, I prefer to ask you :D
重点是,我知道如何做简单的操作符重载,但是我面临堆栈操作符的问题。我将尝试一个相对简单的例子:
The point is, I know how to do simple operator overloading, but I'm facing problems with stacking operators. I'll try to put a relatively simple example:
struct dxfdat
{
int a;
string b;
/* here is the question */
}
/* use: */
dxfdat example;
example << "lalala" << 483 << "puff" << 1029 << endl;
lalala< 483< puff< 1029<< endl
应存储在 b
。
dxfdat&运算符<< (T a)
,并且类似的东西使用一个参数(例如<< 7)
,但我希望它工作在' cout
'时尚。
dxfdat& operator<< (T a)
and things like that work with one parameter (example << 7)
, but I would like it to work in a 'cout
' fashion.
很抱歉懒惰。
EDIT:
真正的东西...好吧,它有点棘手...实际上,b不是字符串,的其他对象, example<< lalala< 483< puff< 1029<< endl
应该只创建一个对象。
The real thing... Ok, it is a little bit trickier... actually, b isn't a string, but a vector of other objects, and example << "lalala" << 483 << "puff" << 1029 << endl
should just create just one object.
这是我正在尝试(翻译),虽然我没有线索如何告诉它是什么时候创建的对象(从左到右,不是吗?):
This is what I'm trying (translated), though I have no clue on how to tell it when to create the object (as it goes from left to right, doesn't it?):
struct dxfDato
{
dxfDato(int c = 0, string v = 0, int t = 0) { cod = c; val= v; ty = t; }
int ty;
int cod;
string val;
};
struct dxfItem
{
int cl;
string val;
vector<dxfDato> dats;
vector<dxfItem> sons;
template <class T>
dxfItem &operator<<(const T &t)
{
dxfDato dd;
std::stringstream ss;
ss << t;
val = ss;
dats.push_back(dd); // this way, it creates a lot of objects
return d;
}
};
dxfItem headers;
headers << "lalala" << 54789 << "sdfa" << 483 << endl;
// this should create *just one object* in dats vector,
// and put everything on string val
感谢一切,
注意:我不得不提取和翻译很多东西放在这里,所以我没有检查代码中的愚蠢错误。
Note: I had to extract and translate a lot of things to put it here, so I didn't check the code for stupid errors.
(对不起,扩大的问题,请告诉我,如果我误用stackoverflow的问题系统)
(Sorry for expanding the question that much, please tell me if I'm misusing stackoverflow's question system)
推荐答案
很容易,不要惊慌:)
它非常类似于 std :: cout
- std :: endl
工作。
You have recognized the problem well: it's very similar to the std::cout
- std::endl
work.
你可以这样做,但如果你不介意,我会重命名类型。
You could do like such, though I'll rename the types, if you don't mind.
struct EndMarker {};
extern const EndMarker end; // To be defined in a .cpp
class Data
{
public:
Data(): m_data(1, "") {}
// Usual operator
template <class T>
Data& operator<<(const T& input)
{
std::ostringstream aStream;
aStream << input;
m_data.back() += aStream.str();
};
// End of object
Data& operator<<(EndMarker) { m_data.push_back(""); }
private:
std::vector<std::string> m_data;
}; // class Data
它默认添加到当前最后一个元素,
It works by adding to the current last element by default, and pushing an empty element at the end.
让我们看一个例子:
Data data;
data << 1 << "bla" << 2 << end << 3 << "foo" << end;
// data.m_data now is
// ["1bla2", "3foo", ""]
另一个解决方案是如果 end
已经流式传输,则保存一个标志(布尔)它有,在下一次插入时创建一个新的元素(并擦除标志)。
The other solution would be to keep a flag (boolean) to store if a end
has been streamed or not, and if it has, creating a new element on the next insertion (and erasing the flag).
插入一点工作,但是没有空元素...您的电话。
It a bit more work on insertion, but you don't have the empty element... your call.
这篇关于C ++操作符重载示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!