简体   繁体   English

来自类的C ++ memcpy数据?

[英]C++ memcpy data from class?

I have a small question, if I have an class with members, for example like this 我有一个小问题,例如,如果我有一个包含成员的课程,例如这样

class CSelectPlayerData : public SqlQuery
{
public:
    BYTE m_Member;
    WORD m_Member2;

    Load_Data(CSelectPlayerData, "SELECT Member, Member2 FROM TABLE);
    MAKE_PARAM(m_Member, m_Member2);
}

What this code does, is that it loads data from sql table, and assigns them to Member and Member2. 该代码的作用是从sql表加载数据,并将其分配给Member和Member2。 My question now is, if I use the query, and do something like this : 现在我的问题是,如果我使用查询,并执行以下操作:

INIT_QUERY(db, CSelectPlayerData) //this also executes the LoadData etc (it's macro)
pPlayer->m_Member = query->m_Member;
...

and then make an separate class, CPlayer 然后创建一个单独的类CPlayer

CPlayer Player;

which has the same parameters (Member, Member2), can I somehow memcpy the values from the query's class to the player, so I don't have to do the 它具有相同的参数(Member,Member2),我可以以某种方式将查询类的值存储到播放器中,所以我不必

pPlayer->m_... = query->m_...

I know it's not really that useful, but it kinda would be just for my additional knowledge. 我知道它并不是真的有用,但这只是为了我的其他知识。 Thanks for answers. 感谢您的回答。

memcpy is safe on any type that has the trait std::is_trivially_copyable. 对于具有特征std :: is_trivially_copyable的任何类型,memcpy都是安全的。

However many classes are not trivially copyable in which case you should use a copy constructor. 但是,许多类不是普通可复制的,在这种情况下,应使用复制构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM