creating a struct in objective c

Member
Posts: 306
Joined: 2009.03
Post: #1
If I want to do this:
struct Tuple2f
{
CGFloat x;
CGFloat y;
Tuple2f (CGFloat x, CGFloat y) : x (x1), y (y1) {}
};
typedef struct Tuple2f Tuple2f;
in .h file and I import it into some .m file, it complains about it. The syntax is supposedly correct, but my question is, is this considered c++ code and would my whole project have to have every m file be mm or am I doing something wrong? I can compile without the constructor so this leads me to think no. The error comes on the line with the constructor and it says syntax before Tuple2f. Even if i change it to an empty constructor its still not happy with it.


I basically want the alternative to
typedef struct Tuple3f
{
CGFloat x;
CGFloat y;
CGFloat z;
} Tuple3f;

struct Tuple3f newTuple3f(CGFloat x,CGFloat y,CGFloat z)
{
struct Tuple3f temp;
temp.x=x;
temp.y=y;
temp.z=z;
return temp;
}

So that I can do in some code:
Tuple3f temp(1,2,3);
vs Tuple3f temp=newTuple3f(1,2,3);


Any ideas?
Thanks.

Oh and P.S My c is a bit rusty. Do i need to overload = operators or will Tuple3f a=b copy the values of all fields by defualt?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #2
It's possible I'm wrong... but your code seems to be a weird mesh of C++ classes and C structs.

You either want:

Code:
typedef struct {
    CGFloat x;
    CGFloat y;
} Tuple2f;

or:

Code:
class Tuple2f {
private:
    CGFloat x;
    CGFloat y;
public:
    Tuple2f (CGFloat x1, CGFloat y1) : x (x1), y (y1) {}
};

Obviously the former is C and the latter is C++. I think you want to do some googling of structs and classes. Structs don't have methods (like the constructor you're trying to put in your Tuple2f struct). Classes do.
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #3
My googling of struct functions is what made me think you can put functions in structs. Is this only true in c++? There are many examples of structs having functions, but I don't know if they were for c or c++
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
kendric Wrote:My googling of struct functions is what made me think you can put functions in structs. Is this only true in c++?

Yes. From my understanding, in C++, class and struct are (almost?) synonymous. You can't put functions in structs in C.
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #5
Ah ok. Thanks
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #6
Hey, so is this how you would suggest doing what I was wanting:
.h
Code:
typedef struct Tuple3f
{
    CGFloat x;
    CGFloat y;
    CGFloat z;
} Tuple3f;

@interface Tuple3fUtil : NSObject
{

}
+(Tuple3f) createWithX:(CGFloat)x y:(CGFloat)y z:(CGFloat)z;
@end


.m
#import "Tuple3fUtil.h"


@implementation Tuple3fUtil
+(Tuple3f) createWithX:(CGFloat)x y:(CGFloat)y z:(CGFloat)z
{
    struct Tuple3f temp;
    temp.x=x;
    temp.y=y;
    temp.z=z;
    return temp;
}
@end
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #7
Once Tuple3f is typedef'd don't use "struct" when declaring a variable of it, like:
Code:
struct Tuple3f temp

Just use:
Code:
Tuple3f temp

Also, I prefer to do something like:
Code:
typedef struct _Tuple3f // <-- note the underscore
{
    CGFloat x;
    CGFloat y;
    CGFloat z;
} Tuple3f;

So that later I can use it as a linked list, like:
Code:
typedef struct _Tuple3f
{
    CGFloat x;
    CGFloat y;
    CGFloat z;
    struct _Tuple3f    *nextTuple3f;
} Tuple3f;

Disclaimer: I don't remember what all the specification rules are on this; it's just the way I've done it for years.


But beyond that, what it looks like you should really do is not use a struct at all in your example. Instead, the struct members should be instance variables. Something like this instead:

Code:
@interface Tuple3fUtil : NSObject
{
    CGFloat x;
    CGFloat y;
    CGFloat z;
}
- (id)initWithX:(CGFloat)inx y:(CGFloat)iny z:(CGFloat)inz;
@end

@implementation Tuple3fUtil
- (id)initWithX:(CGFloat)inx y:(CGFloat)iny z:(CGFloat)inz
{
    if (!(self = [super init]))
        return nil;
    x=inx;
    y=iny;
    z=inz;
    return self;
}
@end
Quote this message in a reply
Member
Posts: 24
Joined: 2009.02
Post: #8
kendric Wrote:So that I can do in some code:
Tuple3f temp(1,2,3);
vs Tuple3f temp=newTuple3f(1,2,3);

My question is why do you want to do this?

Using structs rather than classes often makes good sense for performance reasons, especially if you have lots of short-lived objects. Also, if you're using OpenGL you could pass an array of your tuple structs in a batch directly to OpenGL (via glDrawArrays or glDrawElements). You couldn't do this if they were classes.
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #9
My goal of this was as the last poster said, to improve performance. These objects are gonna get thrown all over the place a lot and created on the fly. So thats why I was using a struct. The reason I wanted the function was a fast way to pass a tuple as a param example
[myObj moveTo:[Tuple3fUtil newTuple:x,y,z]]; instead of having to do
Tuple3fUtil temp;
temp.x=x;
temp.y=y;
temp.z=z;
[myObj moveTo:temp];
Quote this message in a reply
⌘-R in Chief
Posts: 1,254
Joined: 2002.05
Post: #10
Then define functions to manipulate the structs, don't use Obj-C code to do it. Either make it entirely a class and go that route (I don't see why you'd need to here), or just make a struct and a bunch of functions (some likely inlined) that do what you need.
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #11
The point of the class was to organize them. Otherwise ill end up with a ton of global functions. Consider i have tuple2f tuple3f vector2f vector3f etc. If i do the C functions, they all would have to be global right? Just sounds messy, maybe thats my java background talking.
Quote this message in a reply
⌘-R in Chief
Posts: 1,254
Joined: 2002.05
Post: #12
It's really not messy. Everything does not need to be in a class to be organized. You should use the right tool for the job, and here that'd be just some simple functions.

Or, use C++/Obj-C++ and use functions in structs. There's no reason you can't do that.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #13
And just to reiterate what Seth said, it's really not messing. Apple does it all over the place. NSMakePoint, NSMakeSize, NSMakeRect, etc.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  typedef struct - lossing data on some levels at random times jjslay 6 4,137 Jun 26, 2010 02:36 PM
Last Post: jjslay
  Where to place your typedef struct declaration file ? Bracer 1 2,201 Oct 10, 2009 11:10 AM
Last Post: akb825