我正在与图书馆合作开展一个项目,我必须与工会合作。具体来说,我正在与 SDL 和 SDL_Event union 合作。我需要复制 SDL_Events,但找不到有关使用联合重载赋值运算符的良好信息。
假设我可以重载赋值运算符,我是否应该手动筛选联合成员并复制相关成员,或者我可以简单地加入一些成员(这对我来说似乎很危险),或者也许只是使用 memcpy() (这看起来简单而快速) ,但有点危险)?
如果我不能重载运算符,我最好的选择是什么?我想我可以制作新副本并传递一堆指针,但在这种情况下我宁愿不这样做。
欢迎任何想法!
编辑:
按照要求的错误消息,顺便说一句,我想我学到了一些东西......
physworld.cpp:325: 错误:'CurrentEvent = ((physworld*)this)->physworld::SDL_UserInputEvents.std::queue<_Tp, _Sequence> 中与 'operator=' 不匹配: :pop [其中 _Tp = SDL_Event, _Sequence = std::deque; >]()'
/usr/include/SDL/SDL_events.h:220:注意:候选者是:SDL_Event& SDL_Event::operator=(const SDL_Event&)
编辑2:
这太愚蠢了...我以为 Deqeues pop() 成员返回了删除的项目。我以为代码太简单了,不可能直接是我的代码,但事实证明这是错误的。
我的代码看起来像:
for(SDL_Event CurrentEvent; !DequeueOfSDLEvents.empty(); CurrentEvent = DequeueOfSDLEvents.pop() )
{
//Do stuff
}
因此,如果没有别的事情,我将学习更仔细地查看我最近没有使用过的容器的成员函数。感谢您解释默认情况下的分配工作,否则需要更长的时间才能找到它。
I am working on a project with a library and I must work with unions. Specifically I am working with SDL and the SDL_Event union. I need to make copies of the SDL_Events, and I could find no good information on overloading assignment operators with unions.
Provided that I can overload the assignment operator, should I manually sift through the union members and copy the pertinent members or can I simply come some members (this seems dangerous to me), or maybe just use memcpy() (this seems simple and fast, but slightly dangerous)?
If I can't overload operators what would my best options be from there? I guess I could make new copies and pass around a bunch of pointers, but in this situation I would prefer not to do that.
Any ideas welcome!
EDIT:
as requested Errors messages, and incidentally I think I have learned something...
physworld.cpp:325: error: no match for ‘operator=’ in ‘CurrentEvent = ((physworld*)this)->physworld::SDL_UserInputEvents.std::queue<_Tp, _Sequence>::pop [with _Tp = SDL_Event, _Sequence = std::deque<SDL_Event, std::allocator<SDL_Event> >]()’
/usr/include/SDL/SDL_events.h:220: note: candidates are: SDL_Event& SDL_Event::operator=(const SDL_Event&)
EDIT2:
This was so stupid... I thought that Deqeues pop() member returned the item removed. I thought the code was so simple that it couldn't directly be my code, but that turned out to be wrong.
my code looked like:
for(SDL_Event CurrentEvent; !DequeueOfSDLEvents.empty(); CurrentEvent = DequeueOfSDLEvents.pop() )
{
//Do stuff
}
So if nothing else I will learn to look more closely at member functions of containers I haven't used recently. Thanks for explaining assignment worked by default, otherwise It would have taken longer to find this.
发布评论
评论(6)
在联合体中,所有元素都占用相同的内存,就像它们彼此叠置一样。如果写入联合的另一个元素,它会覆盖其他元素。
因此,逐个元素复制是浪费时间。您可以仅复制最大的元素,但是您必须知道那是哪一个(并非并集的每个元素都必须具有相同的大小)最好的做法是仅 memcpy 并集。
但它比这更简单,你应该能够只做一个赋值,编译器意识到你正在复制一个结构或联合,会隐式地为你做“memcpy”。
In a union, the elements all occupy the same memory, like they sit on top of each other. If you write to another element of a union, it overwrites the others.
As such, copying element by element is a waste of time. You could copy the largest element only, but then you would have to know which one that is (not every element of the union has to be the same size) Best thing to do is to just memcpy the union.
But its even simpler than that, you should be able to just do an assignment, and the compiler, realizing you are copying a struct or union, will do the "memcpy" for you implicitly.
由于联合(根据定义)只允许包含 POD,因此您可以安全地使用 memcpy 来复制它们。
Since unions are (by definition) only allowed to contain PODs, then you can safely use
memcpy
to copy them.我可能错了,但是工会不支持开箱即用的分配吗?
看来您也可以像往常一样重载它的operator=,但是您到底希望与正常的默认分配有什么不同?
I may be mistaken, but don't unions support assignment out of the box?
It also appears you can also overload operator= for it as usual, but what exactly would you want to be different than in normal default assignment?
一般来说,您可以只使用编译器生成的赋值运算符。我能想到的唯一例外是,如果您有一个可以包含指针的联合,并且您想为该指针实现深层复制。为了很好地处理这个问题,您必须将其设为可区分的联合,以便您可以确定类型并适当地复制指针。然而,只要您没有远程所有权,编译器生成的赋值(和复制构造函数)就应该可以正常工作。
Generally speaking, you can just use the compiler-generated assignment operator. The only exception I can think of would be if you had a union that could contain a pointer, and you wanted to implement a deep copy for that pointer. To handle that well, you'd have to make it a discriminated union so you could determine the type and copy the pointer appropriately. As long as you don't have remote ownership, however, the compiler-generated assignment (and copy ctor) should work fine.
你不需要做任何特别的事情。在我看来,重载或添加运算符只会增加复杂性。 SDL_event 是一个简单的无符号字符。
You don't need to do anything special. In my opinion overloading or adding operators would only add complexity. The SDL_event is a simple unsigned char.
看起来像 boost::variant
[编辑]
为什么不合适呢?
Looks like a typical use case of boost::variant
[Edit]
Why not appropriate?