0

I have a base class Map which multiple classes inherit from (e.g. France, Germany, Spain, etc). The subclasses simply initialize a constant adjacency list in the base class (via the constructor initialization list) for their respective country, while all functionality is implemented in Map.

As Map should never be instantiated, I have would like to make it an abstract class. However, to do that I would need to declare at least 1 pure virtual function which would then require the derived classes to override. As all functionality is common to all derived classes, it makes sense to keep everything in Map rather than reimplementing in each subclass.

Is there a way around this, or do I need to rethink my design?

19172281
  • 237
  • 2
  • 10
  • Why is this an inheritance relationship at all? – Nathan Pierson Jun 23 '23 at 02:11
  • If your class inherits from an abstract class, and doesn't override any inherited virtual member functions, then your class is also an abstract class. – Peter Jun 23 '23 at 04:06
  • Yes, you need to rethink your design - actually, you're falling into a beginner trap of using inheritance when other approaches are more suitable. `MapOfFrance` does not need to be a different type from `MapOfGermany`. Both can be instances of `Map` (if `Map` is non-abstract, and provides all common capabilities of a `Map`) and data in each map can be populated (e.g. read from a database) differently. – Peter Jun 23 '23 at 04:09
  • Whatever you think Object Oriented doesn't mean "use inheritance for everyhting" (there is even an idiom : "whatever your problem inheritance probably isn't the answer". A map contains countries (at maps of countries) but a map is NOT a country. I hardly use inheritance as my first tool, unless it is inheritance from an interface (abstract baseclass). In almost all other cases composition is the better solution. Code reuse should be done through composition too (yes even it means writing a few lines of simple forwarding code from one class to the next) – Pepijn Kramer Jun 23 '23 at 05:07
  • @Peter, My thinking was that the adjacency list for each country should be tightly coupled to each subclass and only classes that have inherited from Map are legitimate. Allowing the adjacency list to be populated from outside the class breaks this. – 19172281 Jun 23 '23 at 15:09
  • @Peter, Alternatively, I could define the adjacency list for each country within Map and "switch" to the appropriate one in the constructor but this bloats the class. – 19172281 Jun 23 '23 at 15:10
  • So you're planning to hard-code the adjacency list for each country in your program? – Peter Jun 23 '23 at 15:26
  • @Peter, that's the idea – 19172281 Jun 23 '23 at 15:36
  • To give some context, every Map subclass represents an available map in a game. It should be possible to define a new map by deriving from the Map base class, the idea being that the game logic class needn't be concerned with populating the adjacency list, etc. – 19172281 Jun 23 '23 at 16:29

0 Answers0