C ++指定值来POD对象 [英] C++ Assigning Values to POD Objects

查看:129
本文介绍了C ++指定值来POD对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我读<一个href=\"http://programmers.stackexchange.com/questions/30297/is-there-any-reason-to-use-plain-old-data-classes\">Plain旧数据类(POD),并决定让我的结构POD保存数据。例如,我有

So I read about Plain Old Data classes (POD) , and decided to make my structs POD to hold data. For example, I have

struct MyClass {
    int ID;
    int age;
    double height;
    char[8] Name;
};

结果
显然,值分配给结构,我可以这样做:


Obviously, to assign values to the struct, I can do this:

MyClass.ID = 1;
MyClass.age = 20;
...

不过,反正是有指定的原始数据,而不需要知道每个字段的名称?

But is there anyway to assign raw data, WITHOUT knowing the name of each field?

例如,我的程序为每列检索字段值,,我想赋值的结构,因为我不知道该字段的名称。

For example, My program retrieves field value for each column,, and I want to assign the value to the struct, given that i don't know the name of the fields..

MyClass c;

while (MoreColumns()) {
    doSomething( c , GetNextColumn() );    //GetNextColumn() returns some value of POD types
}

我假设有办法做到这一点使用的memcpy,或东西的std ::复制,,但不知道如何下手。

I'm assuming there's way to do this using memcpy, or something std::copy,, but Not sure how to start..

很抱歉,如果这个问题是有点不清楚。

Sorry if the question is a bit unclear.

推荐答案

你所要实现的目标通常会导致难以阅读,甚至无法读取code。但是,假设你有一个真正好的理由来尝试的分配的(而不是初始化),而不知道它的名字原始数据到一个领域,你可以使用reinter pret_cast如下(<一个HREF =htt​​p://ideone.com/DvMmGr相对=nofollow>链接这里)。我不建议这样做,只是想指出,你可以选择。

What you are trying to achieve usually leads to hard-to-read or even unreadable code. However, assuming that you have a genuinely good reason to try to assign (as opposed to initialize) raw data to a field without knowing its name, you could use reinterpret_cast as below (Link here). I don't recommend it, but just want to point out that you have the option.

#include <cstdio>
#include <cstring>

struct Target { // This is your "target"
    char foo[8]; 
};

struct Trap { 
    // The "trap" which lets you manipulate your target
    // without addressing its internals directly.
    // Assuming here that an unsigned occupies 4 bytes (not always holds)
    unsigned i1, i2;
};

int main() {
    Target t;
    strcpy(t.foo, "AAAAAAA");

    // Ask the compiler to "reinterpet" Target* as Trap*
    Trap* tr = reinterpret_cast<Trap*>(&t);

    fprintf(stdout, "Before: %s\n", t.foo);
    printf("%x %x\n", tr->i1, tr->i2);

    // Now manipulate as you please
    // Note the byte ordering issue in i2.
    // on another architecture, you might have to use 0x42424200
    tr->i1 = 0x42424242;
    tr->i2 = 0x00424242;

    printf("After: %s\n", t.foo);

    return 0;
} 

这仅仅是一个简单的例子,我想出了,你可以计算出如何使整洁。请注意,在上面,还可以访问目标迭代,通过使用阵列中的陷阱,而不是I1,I2如我在上面进行。

This is just a quick example I came up with, you can figure out how to make it "neater". Note that in the above, you could also access target iteratively, by using an array in "Trap" instead of i1, i2 as I have done above.

让我再次重申,我不推荐这种风格,但如果你绝对必须这样做,这是你可以探索的一个选项。

Let me reiterate, I don't recommend this style, but if you absolutely must do it, this is an option you could explore.

这篇关于C ++指定值来POD对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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