4

I have a problem in which a variety of classes in C++ will want some functionality that is neither a "has a" nor an "is a" relationship. The problem is that there are some members with associated functions having to do with handling player pointers that some menus and in game objects will need to have. I could copy the functionality across all of them, but I'd rather inject the functionality as advice, so that the functionality is shared. The classes that need the functionality do not share a super in the class tree.

Normally I might use something akin to aspect orientation, making an Aspect that has all the desired functionality and adding it to all of the classes as advice, but because of the C++ codebase at the company I cannot do this.

Is there a good "plain C++" solution to this that I'm overlooking? (c++11 allowed) Right now I hope to use interfaces to do something similar.

edit: i'll be experimenting with each solution, perhaps asking further questions, and will figure out the best solution within the next few days. want to give this thorough consideration for future use.

5
  • Maybe a templated "traits" class that is specialized by the classes that need those attributes? Jun 18, 2015 at 15:06
  • What about using the CRTP? Anyway, it's a bit unclear what you actually want. Jun 18, 2015 at 15:53
  • perhaps this is more clear. i have class A, B:A, C:A, D, E. so some classes extend A, some don't. all of them need to share some functions and members, but the functions and members do not themselves belong in or define any particular class or structure. function pointers could be used or an aggregate class, but i was hoping something akin to aspect orientation but not so complicated as aspects might exist and help to add shared traits to classes without fundamentally changing the class definition by polymorphism.
    – calben
    Jun 19, 2015 at 3:37
  • @calben are those "functions and members" stateful or stateless? If they are stateless, they can be "empty base class static members".
    – rwong
    Jun 19, 2015 at 5:39
  • stateful, sadly..
    – calben
    Jun 19, 2015 at 12:34

2 Answers 2

2

Is all the functionality known at compile time? If so, template metaprogramming might work. See a real example here, but something like this ---

template<class T> ExtraFunctionality {
  //do something with instances of class T
}

template<class T> OtherExtraFunctionality {
  //do something else with instances of class T
}

template< template<class> class Extras = ExtraFunctionality >
class Menu: public AbstractMenu, Extras<Menu> {
  typedef Extras<Menu> Ext;  //for convenience - not required
  //do something with what you inherited from Extras.
}

template< template<class> class Extras = ExtraFunctionality >
class GameObject: public AbstractGameObject, Extras<GameObject> {
  //same deal
}

now you can instantiate

Menu<> menu;

for the Menu with ExtraFunctionality or

Menu<OtherExtraFunctionality>

if you want something different.

This is an example of CRTP mentioned by @Deduplicator.

2

You can try using std::function to pass function pointers around, or just raw function pointers if not (I have used the latter for lookup tables of routines to call based on some data input)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.