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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -