In my custom class, I'm getting an error when declaring the input variable pointer as constant and copying it into a non-constant member variable pointer.
The error I get is:
Cannot initialize a member subobject of type 'Position *' with an lvalue of type 'const Position *'
Why does this happen? And why is this not consistent with the other member variable (_attack
)? It is only copying the value of the pointer (address) the same way it's copying the value of the int (_attack
).
class Creature
{
private:
int _attack;
Position* _position;
public:
Creature(const int attack, const Position* position)
: _attack{attack}, _position{position} // ERROR: Cannot initialize a member subobject of type 'Position *' with an lvalue of type 'const Position *'
{}
};