Pointer to Member Functions

I'm trying to mimic the Get/Set Properties used by Visual Basic and other languages. I've done this by creating a Property class, with the constructor containing parameters for the Parent class's Get/Set functions.

I'm having difficulty with the member-to-function-pointers. From all the searching I've done the following seems like it should be working, but I'm at a loss where to go.

Note: I haven't worked with templates before. Once I get this working with LPCWSTR I was going to try template it and use some typedef to make it clearer.

Here's my 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
#include <windows.h>

class Form;

class Property
{
public:
	Property(LPCWSTR (Form::*Get_Property)(LPCWSTR), LPCWSTR (Form::*Set_Property)(LPCWSTR, LPCWSTR))
	{
		m_Get_Property = Get_Property;
		m_Set_Property = Set_Property;
	};

	Property operator= (LPCWSTR NewValue)
	{
		if (m_Set_Property == NULL)
			m_Property = NewValue;
		else
			m_Property = m_Set_Property(m_Property, NewValue);

		return *this;
	}

	operator LPCWSTR()
	{
		if (m_Get_Property == NULL)
			return m_Property;
		else
			return m_Get_Property(m_Property);
	};

private:
	LPCWSTR m_Property;
	LPCWSTR (Form::*m_Get_Property)(LPCWSTR);
	LPCWSTR (Form::*m_Set_Property)(LPCWSTR, LPCWSTR);
};

class Form
{
public:
	Property Caption(&Form::g_caption, &Form::s_Caption);

private:
	LPCWSTR g_Caption(LPCWSTR OldValue)
	{
		// User called Form.Caption

		return OldValue;
	};

	LPCWSTR s_Caption(LPCWSTR OldValue, LPCWSTR NewValue)
	{
		// User set Form.Caption =

		return NewValue;
	};
};

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	Form fMain;

	fMain.Caption = L"This is a test!";

	MessageBox(NULL, fMain.Caption, NULL, 0);

	return 0;
}
To get a pointer to a member function you need to use class name and the reference operator.
MessageBox(NULL, &Form::Caption, NULL, 0);

EDIT: Or if you are trying to call the Caption function you will need parentheses after the function name.
Last edited on
The MessageBox(NULL, fMain.Caption, NULL, 0) works fine. Caption is a class not a function, so when I can call it without the parentheses it calls the overloaded operator LPCWSTR and correctly returns m_Property.

I am also passing &Form::g_Caption and &Form::s_Caption to the Property class as a constructor, but they are giving me errors.

From further looking, is it possible that &Form::g_Caption cannot be used at this stage as the Form object is still being created, and it might be possible that it does not yet have the addresses for the functions within it?

[EDIT] RESOLVED - Solution:

After more experimenting it does in fact seem that &Form::g_Caption cannot be used until an instance of the class is being created. I have removed the Property constructor parameters and instead they are passed from the Form's constructor, along with a pointer to the Form. I have also made the Property class a friend of the Form so the encapsulation of private values is not exposed beyond the Form.

The following is the working solution:
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
#include <windows.h>

class Form;

class Property
{
public:
	friend class Form;

	Property()
	{
		m_Property = NULL;
		m_Set_Property = NULL;
		m_Get_Property = NULL;
	};

	Property operator= (LPCWSTR NewValue)
	{
		if (m_Set_Property == NULL)
			m_Property = NewValue;
		else
			m_Property = (m_Parent->*m_Set_Property)(m_Property, NewValue);

		return *this;
	}

	operator LPCWSTR()
	{
		if (m_Get_Property == NULL)
			return m_Property;
		else
			return (m_Parent->*m_Get_Property)(m_Property);
	};

private:
	Form *m_Parent;
	LPCWSTR m_Property;
	LPCWSTR (Form::*m_Get_Property)(LPCWSTR);
	LPCWSTR (Form::*m_Set_Property)(LPCWSTR, LPCWSTR);
};

class Form
{
public:
	Property Caption;

	Form()
	{
		Caption.m_Parent = this;
		Caption.m_Set_Property = &Form::s_Caption;
		Caption.m_Get_Property = &Form::g_Caption;
	};

private:
	LPCWSTR g_Caption(LPCWSTR OldValue)
	{
		// User called Form.Caption
		
		return OldValue;
	};

	LPCWSTR s_Caption(LPCWSTR OldValue, LPCWSTR NewValue)
	{
		// User set Form.Caption =
		
		return NewValue;
	};
};

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	Form fMain;

	fMain.Caption = L"This is a test!";

	MessageBox(NULL, fMain.Caption, NULL, 0);

	return 0;
}
Last edited on
Topic archived. No new replies allowed.