0

We recently found an issue with our project caused by this error:

namespace sim
{

   class ClassA
   {
   private:
      static std::list<uint16_t> m_variable;
   }

   std::list<uint16_t> ClassA::m_variable;
}

m_variable became static for all instances of ClassA, not just for the particular instance of it.

Why did this happen?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
phoenixWright
  • 77
  • 1
  • 12
  • I don't understand your question. Your code has no instances of ClassA. And yes, static means only one for the class, not one per instance. – Mat Aug 02 '18 at 07:11
  • _"...became static for all instances of ClassA..."_ expected behaviour. What exactly are you trying to achieve? – Richard Critten Aug 02 '18 at 07:12
  • *"why this happened?"* - Because someone in your project made it a `static` member. – StoryTeller - Unslander Monica Aug 02 '18 at 07:15
  • I'm finding it hard to understand what you mean by _static for a particular instance of ClassA_. You must be using the term static in a different way from C++. In C++ static members are not attached to particular instances of classes, that's what it means. Try explaining what you are expecting with examples. – john Aug 02 '18 at 07:16
  • `static` on a class member means exactly this: the member exists exactly one time, independent of the object/instance, that's **why** you have to define it outside your class. Were you thinking about *static linkage*? That doesn't make sense for instance members, they don't have linkage. –  Aug 02 '18 at 07:22
  • @FelixPalmen - They do have linkage. – StoryTeller - Unslander Monica Aug 02 '18 at 07:25
  • hi all, yes I have misunderstanding of static keyword for classes. Maybe correct question is necessity for this line: std::list ClassA::m_variable; – phoenixWright Aug 02 '18 at 07:29
  • @StoryTeller not by themselves. –  Aug 02 '18 at 07:31
  • If you wonder why you need that line than that has already been asked https://stackoverflow.com/questions/18749071/why-does-a-static-data-member-need-to-be-defined-outside-of-the-class – StoryTeller - Unslander Monica Aug 02 '18 at 07:56

1 Answers1

0

After your comment, you want to understand how to use static members of class.

For normal data members, one instance of the member exists in each and every instance of the class. OTOH a static data member exists only once and is shared among all object instances of the class.

Simply the simple declaration of a static member inside a class definition is only a declaration. Because of the One Definition Rule, that object also needs a definition.

In C++ language, that definition normally occurs outside of the class definition, so the line std::list<uint16_t> ClassA::m_variable; is required.

There are exception to that rule, mainly for static const integral members that can be defined inside the class definition provided you only use their value and not their address (search for One Definition Rule if you want to go further that way). But this is a rather advanced corner case, so if you are a beginner just remember that a static member of a class need a definition outside the class definition.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252