Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

usage of static member as a class typein C++

Tags:

c++

static

Based on "c++ primer", the type of a static data member can be the class type of which it is a member:

class Bar {
 public:
     // ...
 private:
     static Bar mem1; // ok
 };

However, I have a hard time imagining a scenario in which such a feature is useful. I mean, why do you need a variable to be the class type? Can someone give me some examples? Thank you.

like image 722
EXP0 Avatar asked Jan 27 '26 02:01

EXP0


1 Answers

The only time that this would really be useful would be in implementing the "Singleton" pattern (or "Simpleton" to those of us who despise it).

like image 53
Lightness Races in Orbit Avatar answered Jan 28 '26 15:01

Lightness Races in Orbit