About "error: assignment of data-member ‘A::pCost’ in read-only structure".
Hi there,
I got an error as above.
My requirements are: I need a derived class A from class B. In A, there are two member functions, i.e., Initialise() and GetCost(). The Initialise() initialises pointer pCost pointing to an array, and every invocation of GetCost() change the array pCost pointing to.
1. I know that a const function GetCost() can not change the value of the member variable, e.g., pCost. However, I think there is a way to use pointer to solve the problem but I don't know how to do it.
2. The GetCost() in class A is overriden from a virtual function in class B. Can I remove the 'const' after the function?
The codes are shown below, and any suggestions would be appreciated.
Class A : public B
{
public:
void Initialise(void);
void GetCost( const CostType & cost ) const;
protected:
double* pCost;
};
void A::Initialise(void)
{
double iniCost[12] = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 };
pCost = iniCost;
}
void GetCost( const CostType & cost ) const
{
double updatedCost[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
updatedCost = cost; // error as well
pCost = updatedCost; // error: assignment of data-member ‘A::pCost’
}