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;
}
|