0

Assuming I have a global counter to count all derived classes,

extern int classTypeCounter=0;

how can I make them increment the global counter

class A
{
      static void increment();
};

without instantiating/using them? Can this global counter have the number of derived classes before any function/class is used?

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
  • 4
    If your question is "how do I automatically enumerate all the derived classes that are used anywhere in the C++ code I'm compiling" the answer is "no, C++ does not work this way". – Sam Varshavchik Jul 10 '17 at 00:15
  • 3
    What do you want that for? How do you plan to use it? That's a very strange thing to want to do. Sounds like [an XY problem](http://xyproblem.info/) – Igor Tandetnik Jul 10 '17 at 00:16
  • Maybe put a xyzcounter++; in the constructor of A ? – Hannes Hauptmann Jul 10 '17 at 00:17
  • I have N unknown number of derived classes, need to get all their properties before any other classes are used. Not possible? Why no static constructors? There were static code blocks in java. – huseyin tugrul buyukisik Jul 10 '17 at 00:19
  • 4
    _Why no static constructors? There were static code blocks in java._ Umm.. Because C++ is not Java? – Algirdas Preidžius Jul 10 '17 at 00:25
  • @AlgirdasPreidžius: It can have static blocks though - see my answer. – einpoklum Jul 10 '17 at 00:36
  • Trying to "know about all sub-classes" fundamentally violates OO principles. Base classes should **not** need to know _anything_ about their sub-classes. – Disillusioned Jul 10 '17 at 00:45
  • @CraigYoung then there can't be an autonomous factory pattern? I need to add new items by hand? – huseyin tugrul buyukisik Jul 10 '17 at 00:47
  • @einpoklum There's a difference between something that can be implemented, and something that is built-in, and provided by the language itself. Your answer states just what my comment said - it isn't provided by C++ itself, because, well.. It's not Java. Just because there is a solution (or work-around) on this problem is beside the point. – Algirdas Preidžius Jul 10 '17 at 10:15
  • @huseyintugrulbuyukisik imo that should be "autonomous factory ***anti-*** pattern". I'm puzzled why you'd object to _explicitly_ (or "by hand") calling an appropriate function to register a factory _exactly when and where you choose_. I'm sure you'd never seek something that arbitrarily and autonomously adds values just because you happened to include a particular class in your app. I truly see no point in taking on the negatives of autonomous factories to avoid the miniscule (relative to the rest of your codebase) number of lines for explicit registration. – Disillusioned Jul 15 '17 at 03:37

1 Answers1

1

It's probably a bad idea to have such a global variable; and this is probably an "XY problem" like @IgorTendetnik suggests.

However...

You could theoretically use a Java-like static block. C++ doesn't explicitly provide you with that, but - it can be implemented (even in C++98 actually). Then it's a matter of whether you actually have a global variable for a counter or something which itself requires initialization at load-time. But with your class's "API" you would write something like

 static_block { A::increment(); }
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • This looks like I have to call class in this block of code. I need to do it in the class. I mean, does this static block need to be out of class or anywhere it works? – huseyin tugrul buyukisik Jul 10 '17 at 00:31
  • @huseyintugrulbuyukisik: The static block is at file (= translation unit) scope, outside of any class. Now, you could just increment the global counter from the static block, but then it would have to be known outside the class. Plus there might be some static initialization order issues (I don't think so but I'm not sure). – einpoklum Jul 10 '17 at 00:32
  • Then it means I can put it anywhere to work but I can't force it to be implemented for other developers right? There is no pure virtual method to force implementation and at the same time works like this, right? – huseyin tugrul buyukisik Jul 10 '17 at 00:33
  • @huseyintugrulbuyukisik: Yes. But if instead of counting up, you would register your class in a singleton global structure, then you could hide the constructors with a static method that ensures such registration. – einpoklum Jul 10 '17 at 00:35
  • I was already using a singleton to add them myself for every new class. But there are other things than just counting, such as adding their names to a list(pushing into a vector in the singleton) and I don't want to use a singleton for this, thats why I asked if class can load itself. – huseyin tugrul buyukisik Jul 10 '17 at 00:38