C ++联合赋值,有没有一个好的方法来做到这一点? [英] C++ union assignment, is there a good way to do this?

查看:128
本文介绍了C ++联合赋值,有没有一个好的方法来做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个有图书馆的项目,我必须与工会合作。具体来说,我正在使用SDL和 SDL_Event联合。我需要复制SDL_Events,我可以找不到有关重载赋值操作符与工会的好信息。



如果我可以重载赋值运算符,我应该手动通过联合成员和复制相关的成员,或者我可以简单地到一些成员(这似乎是危险的我),或者也许只是使用memcpy()(这看起来简单,快速,但有点危险)?



如果我不能重载运算符,我最好的选择是什么?我想我可以制作新的副本,并传递一堆指针,但在这种情况下,我不想这样做。



任何想法欢迎!

编辑:
请求错误错误的消息,偶然我想我已经学到了一些东西...


physworld.cpp:325:error:在当前事件=((physworld *)this)中没有匹配'operator =' - > 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是:SDL_Event& SDL_Event :: operator =(const SDL_Event&)


EDIT2:
这么蠢...我认为Deqeues pop()成员返回的项目删除。我认为代码很简单,它不能直接是我的代码,但结果是错误的。



我的代码看起来像:

  for(SDL_Event CurrentEvent;!DequeueOfSDLEvents.empty(); CurrentEvent = DequeueOfSDLEvents.pop())
{
//做东西
}

所以如果没有别的,我会学习更密切地看成员函数的容器我最近没有使用。感谢您解释赋值在默认情况下工作,否则需要更长时间才能找到。

解决方案

占据相同的记忆,就像他们坐在彼此的顶部。如果写入联合的另一个元素,它将覆盖其他元素。



因此,逐个元素复制是浪费时间。你可以复制最大的元素,但是你必须知道哪一个是(不是联合的每个元素必须是相同的大小)最好的事情是只是memcpy的联合。



但是它甚至比这更简单,你应该能够做一个赋值,编译器,意识到你复制一个结构或联合,将做memcpy 对你来说。


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.

解决方案

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.

这篇关于C ++联合赋值,有没有一个好的方法来做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆