Search code examples
c++staticstatic-membersone-definition-rule

One Definition Rule and static member initialization


I've read the one definition rule yet could not find the answer to what I'm trying to achieve.

I'm implementing a class in which I need to count every occurrence created of this class, let's name it "Item"

Such that when the header and CPP files are complicated, the static member is defined and with every call to the class' constructor, said static member grows by one and applied to current object that is created. (That is how I presume things are happening 'back stage')

Thanks for any help!


Solution

  • I was using Item::idCounter all along, while I needed to use int Item::idCounter;

    Weirdly, no flags were raised about this.