views:

210

answers:

2

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?

+1  A: 

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;
Alex Reynolds
You'd have to reference it as `struct MyObjectType myobject`, or typedef it as per Carl Norum's reply.
mipadi
+2  A: 

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;
Carl Norum
I get this error I do myType.floatValue1: error: expected identifier or '(' before '.' token
4thSpace
You need to declare a variable... I'll edit the answer.
Carl Norum
did you define a variable like "myType variableName;" and then do "variableName.floatValue1 = 1.0f"? Remember, it's a new type, not a variable itself...
Kendall Helmstetter Gelner
Remove typedef and it seems to work.
4thSpace
@4thSpace, be careful - without the `typedef` that line declares a variable called `myType`, which is a little strange. I edited my answer to show a more expected sort of use case.
Carl Norum