What is the best way to return a class' private character array?

Posted on 2006-04-15
Last Modified: 2010-04-01
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.
Question by:List244
    LVL 86

    Accepted Solution

    Do you want the array to be modifyable by the callers or do you want to keep the original contents? There are several scenarios, assuming the latter I'd probably pick a std::string to handle that transparently if you wnt the original data in your class to be unchanged, e.g.

    #include <string>
    using std::string;

    class foo {


    string get_string () const { return string(m_arr);}


    char* m_arr;


    Thus would allow the caller to fiddle with the data that is returned without changing your original.
    LVL 15

    Expert Comment

    > ...this requires them to have a const array themselves

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now