R
Rennie deGraaf
Let's say that I want to read a method pointer in from a stream. (I'm
not saying that this is a good design idea, or that I actually have a
reason to do this.) If I wanted to read in a function pointer, I could
do something like this:
#include <iostream>
int*(*readFunction())(int, int*)
{
unsigned long x;
std::cin >> x;
return reinterpret_cast<int*(*)(int, int*)>(x);
}
int main()
{
std::cout << reinterpret_cast<unsigned long>(readFunction()) <<
std::endl;
}
I'm assuming here that my input is valid and that an unsigned long is at
least as large as a pointer, but it works on my system.
However, if I try to do something similar to read in a pointer to a
method in a class A, like this:
#include <iostream>
struct A {
int* foo(int, int*)
{
return NULL;
}
};
int* (A::*readMethod())(int, int*)
{
unsigned long x;
std::cin >> x;
return reinterpret_cast<int*(A::*)(int, int*)>(x);
}
int main()
{
std::cout << reinterpret_cast<unsigned long>(readMethod()) <<
std::endl;
}
my compiler (g++ 4.0.2) spits out these errors:
test3.cpp: In function ‘int* (A::* readMethod())(int, int*)’:
test3.cpp:14: error: invalid cast from type ‘long unsigned int’ to
type ‘int* (A::*)(int, int*)’
test3.cpp: In function ‘int main()’:
test3.cpp:20: error: invalid cast from type ‘int* (A::*)(int, int*)’
to type ‘long unsigned int’
I can't simply change ReadMethod() to read in an int*(A::*)(int, int*)
without a cast, since there's no >> operator for that type, and I can't
think of a way to define one without a conversion from some integer type
at some point.
sizeof tells me that an int*(A::*)(int, int*) is 8 bytes on my system,
the same size as an unsigned long long (an g++ extension), but changing
"unsigned long" to "unsigned long long" in the above code doesn't change
the results.
Any suggestions on how to do this?
Thanks,
Rennie deGraaf
not saying that this is a good design idea, or that I actually have a
reason to do this.) If I wanted to read in a function pointer, I could
do something like this:
#include <iostream>
int*(*readFunction())(int, int*)
{
unsigned long x;
std::cin >> x;
return reinterpret_cast<int*(*)(int, int*)>(x);
}
int main()
{
std::cout << reinterpret_cast<unsigned long>(readFunction()) <<
std::endl;
}
I'm assuming here that my input is valid and that an unsigned long is at
least as large as a pointer, but it works on my system.
However, if I try to do something similar to read in a pointer to a
method in a class A, like this:
#include <iostream>
struct A {
int* foo(int, int*)
{
return NULL;
}
};
int* (A::*readMethod())(int, int*)
{
unsigned long x;
std::cin >> x;
return reinterpret_cast<int*(A::*)(int, int*)>(x);
}
int main()
{
std::cout << reinterpret_cast<unsigned long>(readMethod()) <<
std::endl;
}
my compiler (g++ 4.0.2) spits out these errors:
test3.cpp: In function ‘int* (A::* readMethod())(int, int*)’:
test3.cpp:14: error: invalid cast from type ‘long unsigned int’ to
type ‘int* (A::*)(int, int*)’
test3.cpp: In function ‘int main()’:
test3.cpp:20: error: invalid cast from type ‘int* (A::*)(int, int*)’
to type ‘long unsigned int’
I can't simply change ReadMethod() to read in an int*(A::*)(int, int*)
without a cast, since there's no >> operator for that type, and I can't
think of a way to define one without a conversion from some integer type
at some point.
sizeof tells me that an int*(A::*)(int, int*) is 8 bytes on my system,
the same size as an unsigned long long (an g++ extension), but changing
"unsigned long" to "unsigned long long" in the above code doesn't change
the results.
Any suggestions on how to do this?
Thanks,
Rennie deGraaf