Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning Arrays from class functions? 2

Status
Not open for further replies.

RSTR

Programmer
Mar 25, 2000
27
US
I was wondering if it was possible to return a char array from a class function.

I have tried :

char Function(){return string;}

but, of course, that doesn't work. So how do I get it to work? benthecat@hotmail.com

 
> char Function(){return string;}

Well if we assume that the declaration of the class variable 'string' is of type 'char*' then this would work:

char* Function(){return string;}

-pete
 
Aah, I see, the dreaded pointer.


Thankyou very much for your help.

=-)
RSTR benthecat@hotmail.com

 
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

string Function();

string Function(...){
string str;
.
.
.
return str;
}

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.

main()
{
string Astr;
.
.
.
Astr = Function(...); // overloaded copy.
}

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
 
Don't like pointers. Use CString.

CString someclass::somefunc(void){

CString csWasArray;

csWasArray.Format("%s",array);
return csWasArray;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top