Do you see any reason why the pointer should be invalid after the assignment |
this is your code:
errorset_pointer = &errorset::errorset_function;
here
errorset_pointer
is
not a pointer it is a
type just like some int or double
therefore youre assigment looks like for example:
int = 3;
what's not what you want nor compiler want's that...
that why I used typedef to make things easy...
if I wouldn't use typedef then this is what I would have to do:
double(errorset::*errorset_pointer)(double,size_t) myPointer = &errorset::errorset_function;
well this looks unreadble and leads to erros sometimes...
Can you even create a method pointer from within a class during construction?
yes you can but such pointer may be called only withing the class or within class methods or procedures.
another option is to make it public: and acces te type definition by address from outside of class like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
struct errorset {
errorset(const int*);
typedef double(errorset::*errorset_pointer)(double,size_t);
errorset_pointer myPointer;
double errorset_function(double output, size_t t) {return 0.0;}
};
errorset::errorset(const int* x) {
myPointer = &errorset::errorset_function;
}
int main()
{
errorset test(new int);
errorset::errorset_pointer outsidePtr = test.myPointer; //make outside one here
(test.*outsidePtr)(4.4, 4);
return 0;
}
|
hope you understand now :)
also there is one trick with pointer to objects...
continue the code from above WITH ANOTER TYPE OF SINTAX...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
struct errorset {
const int* x;
errorset(const int*);
typedef double(errorset::*errorset_pointer)(double,size_t);
errorset_pointer myPointer;
double errorset_function(double output, size_t t) {return 0.0;}
~errorset()
{
delete x;
}
};
errorset::errorset(const int* x) : x(x){
myPointer = &errorset::errorset_function;
}
int main()
{
errorset* test = new errorset(new int);
errorset::errorset_pointer outsidePtr = test->myPointer; //make outside one here
(test->*outsidePtr)(4.4, 4); //THIS IS HOW YOU CALL POINTER TO POINTER!
delete test;
return 0;
}
|
Good luck with pointers :D they are so powefull