c++ - How to initialize unique variables in base class (Inheritance) -
if declaring several variables in base class, defined in every derived class, right? want have variables in base class once derived class. example:
class base{ public: virtual ~base(); protected: base(); int fileindex; }; class deriveda : public base{ public: deriveda(); virtual ~deriveda(); void dosth(); //using fileindex }; class derivedb : public base{ public: derivedb(); virtual ~derivedb(); void dosthdifferent(); //using same fileindex };
how can that?
just make fileindex
static
variable:
static int fileindex;
then instances derived classes share same value
Comments
Post a Comment