V
Vladimir_petter
Dear All,
There is the problem in nutshells (see the program bellow):
It is ok to convert pointer to F<T> to the pointer to I.
Now if I have pointer to member "F<T> entity::*" can I convert it to the "I
entity::*"?
Compiler does not let me to do that (I've tried on VC 7.1 and Comeau 4.3.3).
If I do reinterpret_cast then the program will be compiled and runs as
expected all though I would like to know possible implications of this
approach.
The goal here to create a static array of fields for a class and be able to
execute algorithms on this array (like the for loop in the main function).
Thanks,
Vladimir.
#include <string>
using namespace std;
struct I
{
virtual char const * foo()=0;
};
template <typename T>
struct F : public I
{
char const * foo()
{
return typeid(T).name();
}
T v_;
};
struct entity
{
typedef I entity::* mem_t;
static mem_t fields[4];
F<int> f1_;
F<char> f2_;
F<string> f3_;
F<double> f4_;
};
entity::mem_t entity::fields[4] = {
reinterpret_cast<mem_t>(&entity::f1_),
reinterpret_cast<mem_t>(&entity::f2_),
reinterpret_cast<mem_t>(&entity::f3_),
reinterpret_cast<mem_t>(&entity::f4_),
};
int main(int, char **)
{
entity e;
for(int i=0; i<4; ++i)
{
printf("%s\n", (e.*(entity::fields)).foo());
}
return 0;
}
There is the problem in nutshells (see the program bellow):
It is ok to convert pointer to F<T> to the pointer to I.
Now if I have pointer to member "F<T> entity::*" can I convert it to the "I
entity::*"?
Compiler does not let me to do that (I've tried on VC 7.1 and Comeau 4.3.3).
If I do reinterpret_cast then the program will be compiled and runs as
expected all though I would like to know possible implications of this
approach.
The goal here to create a static array of fields for a class and be able to
execute algorithms on this array (like the for loop in the main function).
Thanks,
Vladimir.
#include <string>
using namespace std;
struct I
{
virtual char const * foo()=0;
};
template <typename T>
struct F : public I
{
char const * foo()
{
return typeid(T).name();
}
T v_;
};
struct entity
{
typedef I entity::* mem_t;
static mem_t fields[4];
F<int> f1_;
F<char> f2_;
F<string> f3_;
F<double> f4_;
};
entity::mem_t entity::fields[4] = {
reinterpret_cast<mem_t>(&entity::f1_),
reinterpret_cast<mem_t>(&entity::f2_),
reinterpret_cast<mem_t>(&entity::f3_),
reinterpret_cast<mem_t>(&entity::f4_),
};
int main(int, char **)
{
entity e;
for(int i=0; i<4; ++i)
{
printf("%s\n", (e.*(entity::fields)).foo());
}
return 0;
}