Need help with method pointer

I have a class called errorset, which contains a function and a pointer to that function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct errorset {
  // the constructor sets errorset_pointer to errorset_function
  errorset(const int*);

  double(errorset::*errorset_pointer)(double,size_t); // pointer

  // function:
  double errorset_function(double output, size_t t) {return 0.0;}
};

// now the constructor:
errorset::errorset() {
  errorset_pointer = &errorset::errorset_function;
  // breakpoint here:
  // errorset_pointer = 0xcdcdcdcd
}


I'm new to method pointers so it's not really apparent to me what I'm doing wrong, and would appreciate some guidance as to why the errorset pointer value is invalid..
Last edited on
I hope this code will explain you the sintax...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct errorset {
  errorset(const int*);
  double errorset_function(double output, size_t t) {return 0.0;}
};

typedef double(errorset::*errorset_pointer)(double,size_t); //added typedef 
const int* num = new const int; //added argument
errorset_pointer myPointer; //creating pointer

errorset::errorset(const int*) { //added argument const int*
}

int main()
{
	myPointer = &errorset::errorset_function; //initialized new pointer
	errorset test(num);//creating object
	(test.*myPointer)(4.4, 4); //this is how you call your pointer
	delete num;
	return 0;
}


if you have more qustions fell free to ask :D
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
Last edited on
Why did u delete your second post?
if this doesn't help you then tell so, maybe other members can give u better answer :/
Last edited on
double(errorset::*errorset_pointer)(double,size_t) That creates a pointer to a member function. It's name is errorset_pointer and its type is: 'pointer to member function that expect a double and a size_t and returns a double'

double(errorset::*errorset_pointer)(double,size_t) myPointer; That doesn't compile.
Buy yeah, typedefs make it clear.

@OP: ¿why do you say that the pointer is invalid?
¿how do you expect to use it? (obj.*(obj.errorset_pointer))(3.14, 42); looks ugly.

@codekiddy: you love memory leaks ¿don't you?
Oh thanks,
double(errorset::*errorset_pointer)(double,size_t) myPointer; That doesn't compile.


I don't think so.. even if I din't check on my compiler if it relay compiles or not.

you love memory leaks ¿don't you?


I've corrected my memory leak in second example but thanks for being able seing that :D
Topic archived. No new replies allowed.