关于error: assignment of data-member ‘A::pCost’ in read-only structure的问题
关于error: assignment of data-member ‘A::pCost’ in read-only structure的问题
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;
pCost = updatedCost; // 此行报错:error: assignment of data-member ‘A::pCost’
}
请教问题出在哪里?
需求是在A::Initialise初始化pCost指针,使其指向一个初始数组,然后在GetCost函数中更新pCost指向的数组。
函数GetCost( const CostType & cost ) const中的const无法去掉(可以去掉吗?),A继承自B得来。
谢谢!