[Solved] C++ Templates – Passing Class member [closed]


It seems you want to pass a non-type template parameter, and more specifically, a pointer-to-member. For what you want, you have to user:

template<ObjectType gameclass::* ptr>
struct foo
// ...

Note that will allow you to access the member of an object only if you have a pointer to that object.

You can have more details here (pointer-to-member as template parameter) or here (pointer-to-members).

Note that you cannot pass the value of mygame.a to a template in the code you posted, because:

  1. template parameters are compile-time, your value is runtime.
  2. non-type template parameters cannot be any object type, only integral (or enum), pointer/reference to object/function, or pointer-to-member.

EDIT:
You have changed your template to template<ObjectType& x>. This is correct for reference to object. However, mygame.a is not a reference, it is a value. To specify the member, you have to use the syntax &mygame::a, which forces you to use a pointer-to-member.

solved C++ Templates – Passing Class member [closed]