4thSpace
4thSpace

Reputation: 44310

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. Or must I create a class?

Upvotes: 0

Views: 798

Answers (2)

Carl Norum
Carl Norum

Reputation: 224864

Sure, just make a C structure:

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;

Upvotes: 2

Alex Reynolds
Alex Reynolds

Reputation: 96937

Take a look at using a struct, e.g.:

struct MyObjectType {
    float floatValue1;
    float floatValue2;
};

...

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

Upvotes: 1

Related Questions