I'm wondering how can I make unmodifiable a parent class member which will be different for each derived class.
My code now assigns its value correctly (depending on the Child class that calls the parent class' constructor), but since I'm not being able to declare it as either static or const, m_type can be easily modified (in myfunction, for example) and that's what I'd like to avoid.
#include <iostream>
enum Piece_type{ king=1, queen=3, rook=3, bishop=4, knight=5, pawn=6};
class Piece
{
protected:
Piece_type m_type; // static Piece_type m_type; <- doesn't work,
// and neither static const or const do
Piece(Piece_type ex): m_type(ex) {}
};
class Pawn: public Piece
{
public:
Pawn():Piece(pawn) {} // To initialise m_type as pawn for all my Pawn objects
void myfunction()
{
std::cout<<"My piece type is "<< m_type<<std::endl;;
m_type= knight; // This is the assignation I want to avoid happening
std::cout<<"My new piece type i "<<m_type<<std::endl;
}
};
My question is related to this one, but inheritance doesn't seem to make possible to declare a static variable and define its value through a member initializer.
I've found how to call the parent/base class constructor from the Child class in this question.
Thanks in advance,
Eduardo
Aucun commentaire:
Enregistrer un commentaire