Sign in to follow this  

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

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 enough
class MyClass {
public:
struct my_stuff_t;
my_stuff_t getNum();
private:
struct my_stuff_t { int num[4]; }
};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this