简体   繁体   English

枚举或带有类型的typedef

[英]Enum or typedef with types

I'd like to create a simple object that I can access like this: 我想创建一个可以像这样访问的简单对象:

myobject.floatValue1 = 1.0;
myobject.floatValue2 = 2.0;

It shouldn't have anymore than the two properties. 它不应该具有两个以上的属性。 Is it possible to create an enum or typedef with such a simple structure. 是否可以用这种简单的结构创建枚举或typedef。 Or must I create a class? 还是我必须创建一个类?

Sure, just make a C structure: 当然,只需创建一个C结构:

struct myStruct 
{
    float floatValue1;
    float floatValue2;
};
typedef struct myStruct myType;

Then use it like this: 然后像这样使用它:

myType myVariable = {0.0, 0.0}; // optional initialization
myVariable.floatValue1 = 1.0;
myVariable.floatValue2 = 2.0;

Take a look at using a struct , eg: 看一下使用struct ,例如:

struct MyObjectType {
    float floatValue1;
    float floatValue2;
};

...

MyObjectType myobject;
myobject.floatValue1 = 1.0;
myobject.floatValue2 = 2.0;

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

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