Brian
Brian

Reputation: 189

Return pointer to array

I'm trying to return a pointer to an array for my function prototype.

class NAV                  
{
    string date;
    float nav;
public:
    NAV(const string&);  
};

const int HistorySize = 300;

class MutualFund
{
    string ticker;
    NAV* history[HistorySize];
public:
    MutualFund(const string& ticker_, const string& historyFile);
    ~MutualFund();
    NAV** getArray() const{return history;}
    void report() const;

};

For NAV** getArray() const{return history;}, I'm getting a compiler error:

error: invalid conversion from 'NAV* const*' to 'NAV**' [-fpermissive]

Any ideas?

Upvotes: 1

Views: 111

Answers (2)

Aiken Drum
Aiken Drum

Reputation: 593

Your getter is a const method, so during its execution, all data members are considered const as well. That's why the conversion error says it is converting from const to non-const, since your return value is non-const.

Upvotes: 0

user4581301
user4581301

Reputation: 33931

In NAV** getArray() const{return history;} const means the programmer promises that calling this function will not cause changes to the state of the MutualFund. By returning a non-const pointer, NAV**, you are opening up the possibility for the state to be changed through use of the returned pointer. The compiler will not allow this and is telling you that it can only return a pointer to constant data: NAV* const*.

Upvotes: 4

Related Questions