List244
asked on
What is the best way to return a class' private character array?
If a class has a private array member, what is the best way to return this?
Currently, I have it returning as const char* so that they may not alter it,
however this requires them to have a const array themselves, and I don't
know that I like that.
I also have tried a static variable as the return instead, however with this I
don't know that I like the memory being held until my application ends.
Then, I know I could return a string, or use new/delete... But at this point
I figured I would just come and get some input on what all of you feel is the
best way to go about doing such a thing.
Currently, I have it returning as const char* so that they may not alter it,
however this requires them to have a const array themselves, and I don't
know that I like that.
I also have tried a static variable as the return instead, however with this I
don't know that I like the memory being held until my application ends.
Then, I know I could return a string, or use new/delete... But at this point
I figured I would just come and get some input on what all of you feel is the
best way to go about doing such a thing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why? For read access, a client can use the pointer to const char, and to change the string, the client would have to copy it to a non-const array.
For the greatest efficiency, return a pointer to const char. For the greatest simplicity, convenience, and safety, return a string, as jkr suggested.