Click here to Skip to main content
15,072,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i like to do some basic stuff in c++. I want to have one solely base class and many child class whome all are derived from the base class. So in the base class i like to have a member that just counts up each time there is a new child class. So the base class should be for all child classes the same and not initialize several times again as a new one. Can anyone give me a hint.

Thanks

What I have tried:

class App {
private:

	int	count = 0;

public:
	App(void);
	App(int);
	virtual ~App(void);

	int getCount(void);
};

class Derived : virtual public App {
public:
    Derived();

}

int main()
{
    Derived d1;//count 1
    Derived d2;//count should be 2 now
}
Posted
Updated 13-Oct-21 4:30am

   
Comments
CPallini 13-Oct-21 9:21am
   
5.
Greetings Kind Regards Merely declare your base class counter static - Cheerio
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900