Calling a defined function pointer from another pointer to a class object......

Hello,

I am attempting to implement function pointers and I am having a bit of a problem.

See the code example below; what I want to be able to do is call a function pointer from another pointer.

I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.

I hope the example helps.

Thank you for your time.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class MainObject;
class SecondaryObject;

class SecondaryObject {
	public:
		void FirstFunction();
		void SecondFunction();
		void (MainObject::*FunctionPointer)();
		void SwitchToSecondFunction();
		void SwitchToFirstFunction();
		SecondaryObject();
		~SecondaryObject();
};

void SecondaryObject::SwitchToSecondFunction() {

	FunctionPointer = &SecondaryObject::SecondFunction;

}

void SecondaryObject::SwitchToFirstFunction() {

	FunctionPointer = &SecondaryObject::FirstFunction;

}

SecondaryObject::SecondaryObject() {

	FunctionPointer = &SecondaryObject::FirstFunction;

}

class MainObject {
	public:
		SecondaryObject* GetInnerObject();
		MainObject();
		~MainObject();
	private:
		SecondaryObject* InnerObject;
	
};

MainObject::MainObject() {

	InnerObject = new SecondaryObject();

}

MainObject::~MainObject() {

	delete InnerObject;

}

SecondaryObject* MainObject::GetInnerObject() {

	return InnerObject;

}

int main(int argc, char** argv) {
	
	MainObject* OuterObject;
	
	/*
	
	How can I do something like the following below based on the code above?
	
	*/
	
	OuterObject->GetInnerObject()->FunctionPointer();
	OuterObject->GetInnerObject()->SwitchToSecondFunction();
	OuterObject->GetInnerObject()->FunctionPointer();
	
	/*
	
	End of Example.
	
	*/
	
}

There are no function pointers in your code. There are, however, member function pointers.

Line 8: void (SecondaryObject::*FunctionPointer)();

Line 71: OuterObject->GetInnerObject()->*(OuterObject->GetInnerObject()->FunctionPointer)();
(untested)
Last edited on
Thanks, @LB.

Not sure what I am doing wrong but the syntax:

 
OuterObject->GetInnerObject()->*(OuterObject->GetInnerObject()->FunctionPointer)();


Isn't working.

I get the following error:


error: expression preceding parentheses of apparent call must have (pointer-to-) function type


Is there something different I need to change it too?

Thank you.
Tried to compile the exact code above, here is the error I get:

error C2064: term does not evaluate to a function taking 0 arguments

On this line:

OuterObject->GetInnerObject()->*(OuterObject->GetInnerObject()->FunctionPointer)();

Here is a complete code example:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>

class MainObject;
class SecondaryObject;

class SecondaryObject {
public:
	void FirstFunction();
	void SecondFunction();
	void (SecondaryObject::*FunctionPointer)();
	void SwitchToSecondFunction();
	void SwitchToFirstFunction();
	SecondaryObject();
	~SecondaryObject();
};

void SecondaryObject::SwitchToSecondFunction() {

	FunctionPointer = &SecondaryObject::SecondFunction;

}

void SecondaryObject::SwitchToFirstFunction() {

	FunctionPointer = &SecondaryObject::FirstFunction;

}

SecondaryObject::SecondaryObject() {

	FunctionPointer = &SecondaryObject::FirstFunction;

}

void SecondaryObject::FirstFunction() {

	std::cout << "Output from the first function. \n";

}

void SecondaryObject::SecondFunction() {

	std::cout << "Output from the second function. \n";

}

class MainObject {
public:
	SecondaryObject* GetInnerObject();
	MainObject();
	~MainObject();
private:
	SecondaryObject* InnerObject;

};

MainObject::MainObject() {

	InnerObject = new SecondaryObject();

}

MainObject::~MainObject() {

	delete InnerObject;

}

SecondaryObject* MainObject::GetInnerObject() {

	return InnerObject;

}

int main(int argc, char** argv) {

	MainObject* OuterObject;

	/*

	How can I do something like the following below based on the code above?

	*/

	OuterObject->GetInnerObject()->*(OuterObject->GetInnerObject()->FunctionPointer)();
	OuterObject->GetInnerObject()->SwitchToSecondFunction();
	OuterObject->GetInnerObject()->*(OuterObject->GetInnerObject()->FunctionPointer)();

	/*

	End of Example.

	*/

}

Oh, it just needs an extra set of parens:

(OuterObject->GetInnerObject()->*(OuterObject->GetInnerObject()->FunctionPointer))();

http://coliru.stacked-crooked.com/a/26621e5f17837495
Last edited on
And now you may realize that that is ugly, convoluted and error prone.
There has to be a better way to accomplish what you want to do

¿what do you want to do? (emphasis in what, not in how)
Thanks, @LB! It works.

@ne555, what I want to do is set up a way where I don't have to have an if/else if type of setup to call different functions.

My "real" program is a 3D engine where I want to switch to different types of cameras based on the user's preferences.

So, as you know, with 3D engines there is perspective and orthographic cameras. Let's pretend that in the code above "FirstFunction" is the perspective-based camera and the "SecondFunction" is the orthographic-based camera.

As it stands now on every frame I have to do an if/else if statement on whether to run parameters and calculations for the type of camera it is.

With the function pointer what I was hoping to achieve was to avoid the if/else if test on every frame and just update the function pointer which will allow me to call the right function.

I hope this makes sense. I can give a code example that more closely aligns with what I am trying to achieve with the function pointers.

Thank you for your time.
You want camera objects, not camera functions.
I agree; I do have camera objects.

Are you saying I should have two separate classes for each type of camera?

Right now I have a single class for the camera which has perspective and orthographic functions.
I am saying you should have one class for each type of camera and one base class for all cameras.
Makes sense; then you would create over-ridable functions for the updating of each camera.

Will give that a try.
Topic archived. No new replies allowed.