c++, In a function how would I return 'int num[4]' ?

Started by
9 comments, last by Zahlman 19 years, 2 months ago
Hi, I've got a class and I'm trying to return an integer array of 4. How would I return it in a get function?

class MyClass {
private:
   int num[4];
public:
   //how would I return num[4]?
   int[4] getNum();//??
};


Would I just say int* getNum(){return num;} ? But then how would i put it back into an array of 4? int num[4] = mc.getNum()? Thanks
Advertisement
You would return the pointer. Remember that arrays are pointers.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
Can't return arrays in C/C++; like above said - you must return a pointer.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Quote:Original post by SumDude
You would return the pointer. Remember that arrays are pointers.

Arrays are not pointers, they are entirely different.
You must return by pointer, but then the caller has access to the same array (essentially reference-passing), while you probably want a value return. Use a std::vector instead of the array; it will solve your problems.
If your member protype was this:

int* getNum(){return num;}

and example of main could be:

void main(void){   MyClass Test;   int* pNum = NULL;   pNum = Test.GetNum();   // either method below would work.   cout << pNum[0] << pNum[1] << pNum[2] << pNum[3];   cout << *(pNum + 0) << *(pNum + 1) << *(pNum + 2) << *(pNum + 3);}


This would work, however you must make sure that you do not accidently access an element beyond the bounds of the array, since C/C++ does not have array bounds checking. Doing so would potentially overwrite data in other variables. Also if you class would happen to go out of scope the data would no longer be valid. Someone else suggested using the vector stl class. The vector stl class might be a better way to do this since it has array bounds checking and other built in functionality. Hope this helps a lil.

- Bill
What is your class? What information are you trying to return? There are a few potential issues here. Returning a pointer can be dangerous and will break encapsulation (if that's important to you). Returning a std::vector<> by value will incur some overhead and involve some memory allocation - again, that may or may not be important to you. You could return a reference to an internal std::vector<>, but again the caller could keep that reference around past the lifetime of the object.

If you really need to pass this internal information on, something like this would probably be better:

void Class::GetData(float[] out) const
{
assert(out);
// Copy num[] into out[]
}

Or:

void Class::GetData(std::vector<int>& out) const
{
out.clear();
// Copy num[] into out
}

This puts the responsibility of memory allocation and whatnot on the caller.

Finally, though, if you're needing to pass an entire array back from inside a class, you may have a design issue. (One place I can think of this being useful, though, would be for example extracting an OpenGL-style float[16] array from a matrix class.)
Arrays aren't assignable, or copyable. You can however get away with returning a reference to an array:

class MyClass {private:   int num[4];public:      int (&getNum())[4]    {     return num;    }};


Returning a pointer would look a lot more normal, though, and probably wouldn't make any difference. That's a pretty funky function declaration if I do say so myself.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
You could also wrap the array member in a struct (which is basically what the use of std::vector would accomplish here, although then you would get a bunch more for free too):

// I think this is good enoughclass MyClass {public:   struct my_stuff_t;   my_stuff_t getNum();private:   struct my_stuff_t { int num[4]; }};
Quote:Original post by Zahlman
(which is basically what the use of std::vector would accomplish here, although then you would get a bunch more for free too)
Dynamic allocation isn't free. Your method is likely to be *much* faster should return by value be used.

This topic is closed to new replies.

Advertisement