0

I suppose this is an unusual use case. But I've run into it a few times now.

Is it possible to force all inherited classes to have public static variables that are defined in the child class?

Aka:

APacket.ID   = 0; // Abstract parent class, has static ID variable (0)
PacketPos.ID = 1; // Inherits from APacket, has static ID variable (1)
PacketVel.ID = 2; // Inherits from APacket, has static ID variable (2)

I want all my network packet classes to have unique and public static ID and Length variables.

Is something like this possible?

Leif
  • 107
  • 1
  • 11
  • No. Inheritance is about controlling _instantiated_ objects. Static is explicitly about non-instantiated entities. – gunr2171 Nov 09 '18 at 18:19
  • @gunr2171 Static members *are* inherited though. There's just no virtual dispatch of static members, and as a result there is no support for polymorphism of static members. – Servy Nov 09 '18 at 18:21
  • I think this question is not duplicate. The OP is asking about unique static variables in derived classes with same name – Pavan Chandaka Nov 09 '18 at 18:29
  • Thanks for the feedback. I feel as though forcing static members should be supported. It would be quite useful in numerous situations. – Leif Nov 09 '18 at 19:14

0 Answers0