dimanche 17 mai 2015

Propagate an enum class to a higher level class

This is a fairly basic problem but I'd like some input on what the best practice is. My issue is how do you propagate an enum from a sub-class to a higher level class so that it is visible to the main class's caller?

For example, say I have a class Bike. Bike will initialize sub-classes Wheel, Frame, Chain, etc. that make up a bike. Frame might have an enum such as FrameColor, Chain might have an enum such as LinkType, etc. Now let's say some higher level code wants to construct a Bike class. The constructor of Bike would require the caller to specify the LinkType, FrameColor, etc. I'd like to avoid putting the enum definitions inside a global header file, since this is bad C++ style.

Two possible approaches I've thought of but there must be a simpler, cleaner way: -Use an abstract class defining an interface -Forward declare the enum

Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire