![]() |
|
Two c++ questions - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: Two c++ questions (/thread-4755.html) |
Two c++ questions - Najdorf - Nov 26, 2005 07:03 PM 1) How do I do a function that returns an array (not the pointer, the whole array): I tried something like Code: int function()[]then calling it with Code: int foo[5]=function();but it doesnt seem to work. 2) Say I have a "Ragdoll" class that is composed by 10 "Balls". Now, I want every ball to have a pointer to the Ragdoll of which it's a part of. So I would have Code: class RagdollCode: class BallBut the compiler does not like this, since the "Ball" class needs the ragdoll class to be defined and viceversa. How should I do it? Thanks. Two c++ questions - OneSadCookie - Nov 26, 2005 08:36 PM 1) either use std::vector, or new -- Code: int *function() {Note that that makes it the caller's responsibility to delete [] the results. 2) forward-declared the Ragdoll class -- Code: class Ragdoll;Two c++ questions - akb825 - Nov 26, 2005 08:52 PM Basically, you can't. You have to create dynamic memory and return the pointer to that. Or you can use a global array. Or a static array that you return the pointer to, but that will get overwritten, along with a global array. You could, however, pass in an already created array and have the function edit it. Two c++ questions - NCarter - Nov 27, 2005 08:15 AM Just to elaborate on what Keith said, you can get the exact result you're looking for with std::vector: Code: std::vector<Class> Function()Two c++ questions - Najdorf - Nov 27, 2005 04:43 PM Thanks. >You could, however, pass in an already created array and have the function edit it. I think i'll stick to this one, thanks Two c++ questions - zKing - Dec 9, 2005 10:22 PM Also in C/C++ pointers and arrays are (mostly) interchangable, i.e.: Code: int* function()WARNING: Playing with pointers like this can ruin your day if you don't know what you are doing... and sometimes even when you do. This works because an array is just a spot in memory with the values in a contiguous row: A line like this: int foo[5]; Will create something like this: | int0 | int1 | int2 | int3 | int 4 | When you do this: int* bar = foo; The "bar" pointer points to the address of the first element in the foo array (int0 in this case). Code like "bar[3]" and "foo[3]" both translate to the same thing: Take the base address (foo or bar) and add three times 'sizeof(int)' to it. You run into trouble if foo/bar are NULL, garbage or you go running off the end of your array... so be careful. The compiler will be quite happy to generate code for this: int bla[10]; bla[1234] = 0; But lord knows where that zero is going to land in memory, if you are lucky it just crashes, if not you get strange "why is my program doing that?" bugs. For your case you could try something like this: Code: class Ragdoll; |