![]() |
|
creating a struct in objective c - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html) +--- Thread: creating a struct in objective c (/thread-1654.html) |
creating a struct in objective c - kendric - Mar 9, 2009 06:45 PM 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? creating a struct in objective c - Josh - Mar 9, 2009 07:16 PM 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 {or: Code: class Tuple2f {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. creating a struct in objective c - kendric - Mar 9, 2009 07:39 PM 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++ creating a struct in objective c - ThemsAllTook - Mar 9, 2009 07:52 PM 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. creating a struct in objective c - kendric - Mar 9, 2009 08:41 PM Ah ok. Thanks creating a struct in objective c - kendric - Mar 9, 2009 08:57 PM Hey, so is this how you would suggest doing what I was wanting: .h Code: typedef struct Tuple3fcreating a struct in objective c - AnotherJake - Mar 9, 2009 10:42 PM Once Tuple3f is typedef'd don't use "struct" when declaring a variable of it, like: Code: struct Tuple3f tempJust use: Code: Tuple3f tempAlso, I prefer to do something like: Code: typedef struct _Tuple3f // <-- note the underscoreSo that later I can use it as a linked list, like: Code: typedef struct _Tuple3fDisclaimer: 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 creating a struct in objective c - mpatric - Mar 10, 2009 03:34 AM kendric Wrote:So that I can do in some code: 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. creating a struct in objective c - kendric - Mar 10, 2009 07:36 AM 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]; creating a struct in objective c - SethWillits - Mar 10, 2009 12:52 PM 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. creating a struct in objective c - kendric - Mar 10, 2009 02:54 PM 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. creating a struct in objective c - SethWillits - Mar 10, 2009 03:15 PM 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. creating a struct in objective c - Josh - Mar 10, 2009 03:36 PM And just to reiterate what Seth said, it's really not messing. Apple does it all over the place. NSMakePoint, NSMakeSize, NSMakeRect, etc. |