When writing member functions that return "large things" like arrays, a programmer has two choices:
(1) Send the array back via its pointer.
(2) Push the entire array onto the stack.
Either way you go, make sure you are ready to receive the return value in all its grandeur. Say 'string' is a class with some char array as a data member like so:
class string {
public:
char data[50];
.
.
.
};
Way Number 1: Return the array via its pointer.
------------------------------------------------
The member function 'Function' must take this form:
char * Function();
char * string::Function(){ return data; }
If its not a member function, it must take this form:
char * Function(string str){ return str.data; }
If data is not a public member, chances are there is some public function that will give you a char pointer. Such as str.GetString().
On the receiving end, you must be ready to get a char pointer when calling 'Function()'
main() {
char *cptr;
string str;
.
.
.
cptr = str.Function(...);
}
Way Number 2: Push the whole thing onto the stack.
-------------------------------------------------
Before you do this, make sure the string class has an overloaded copy operator. (=)
The member function 'Function' must take this form
Notice that even though str loses scope at the end of Function end is hence, deconstructed, you can still return it, and even USE it in the calling function. This works because the entire class was pushed onto the stack.
Make sure you are ready at the receiving end.
Before the days of C++, C compilers wouldn't even let you return arrays. With the advent of classes and overloading, you can return gigantic data from functions (but this is normally considered bad technique.)
-
Paris, 1849
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.