class MyClass {
private:
int num[4];
public:
//how would I return num[4]?
int[4] getNum();//??
};
c++, In a function how would I return 'int num[4]' ?
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?
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
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:
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
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.)
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:
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.
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.
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 ZahlmanDynamic allocation isn't free. Your method is likely to be *much* faster should return by value be used.
(which is basically what the use of std::vector would accomplish here, although then you would get a bunch more for free too)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement