I came across this code and was wondering what it means. But even after some 15 minutes of looking at it does not make sense to me.
template< typename T >
struct Vector4 {
typedef T Vector4<T>::* const vec[4];
static const vec constVec;
//just to have some member instances of T
T member1, member2, member3, member4;
};
So what is the type of constVec? Please do not just repeat the typedef but explain in common language.
My notes so far:
T
and Vector4<T>
), is this a function pointer?constVec
is an array of 4 constant pointers to the members of the Vector4<T>
class which are of type T
Note: The members aren't constant, the pointers themselves are.
First, since these are constant pointers, you need to initialize them in the constructor: (I've just noticed the static
qualifier, so it has to be initialized outside the class, but if it weren't static, you'd need to do that in the initialization list.)
template< typename T >
struct Vector4 {
typedef T Vector4<T>::* const vec[4];
static const vec constVec;
//just to have some member instances of T
T member1, member2, member3, member4;
};
template<typename T>
const typename Vector4<T>::vec Vector4<T>::constVec = {&Vector4::member1,&Vector4::member2,&Vector4::member3,&Vector4::member4};
int main() {
Vector4<int> v;
for(int i=0; i<4; i++) {
(v.*Vector4<int>::constVec[i]) = 5;
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With