static class member - access to variables

T

tom

How to get access to veriable from static class member?



class cTest
{
public:
int a;
void Set_a(){a = 1;}
static int dlgTest()
{
//a = 1; // how to get access to 'a' if 'dlgTest()' is static
return 1;
}
};


// how in 'dlgTest' function change variable 'a'
// 'dlgTest' - I nead this for: INT_PTR nRet = DialogBox(hInstance,
MAKEINTRESOURCE(IDD_LOGIN), NULL, (DLGPROC)dlgTest);
// I'am forced to declare it as static ( as I think)
// static BOOL CALLBACK dlgTest(HWND hWnd, UINT msg, WPARAM wParam, LPARAM
lParam)

I'm using VC++ .NET

t.
 
D

dasjotre

tom said:
How to get access to veriable from static class member?



class cTest
{
public:
int a;
void Set_a(){a = 1;}

use constructors to initialize members
i.e. cTest(int aa): a(aa){}
static int dlgTest()
{
//a = 1; // how to get access to 'a' if 'dlgTest()' is static

a is a non static member and each instance of cTest
will have it's private a. You would have to pass an
instance of cTest to it to access it's a:

static int dlgTest(cTest & t)
{
t.a = 1;
}

so in:
cTest t1(1);
cTest t2(2)

cTest::dlgTest(t1); // can aceess t1's a
cTest::dlgTest(t2); // can aceess t2's a
return 1;
}
};


// how in 'dlgTest' function change variable 'a'
// 'dlgTest' - I nead this for: INT_PTR nRet = DialogBox(hInstance,
MAKEINTRESOURCE(IDD_LOGIN), NULL, (DLGPROC)dlgTest);
// I'am forced to declare it as static ( as I think)
// static BOOL CALLBACK dlgTest(HWND hWnd, UINT msg, WPARAM wParam, LPARAM
lParam)

I'm using VC++ .NET

This is windows GUI specific question.
Look up ATL::CDialogImpl or MFC::Cdialog
 
N

Noah Roberts

tom said:
How to get access to veriable from static class member?



class cTest
{
public:
int a;
void Set_a(){a = 1;}
static int dlgTest()
{
//a = 1; // how to get access to 'a' if 'dlgTest()' is static
return 1;
}
};


// how in 'dlgTest' function change variable 'a'
// 'dlgTest' - I nead this for: INT_PTR nRet = DialogBox(hInstance,
MAKEINTRESOURCE(IDD_LOGIN), NULL, (DLGPROC)dlgTest);
// I'am forced to declare it as static ( as I think)
// static BOOL CALLBACK dlgTest(HWND hWnd, UINT msg, WPARAM wParam, LPARAM
lParam)

You can't do what you are trying to do. For one thing there is no way
to access a in dlgTest. For another thing, and this breaks away from
C++ proper, is that your dlgproc is not correctly declared or used. An
alternative approach is something like this:

template <typename Impl>
class Dialog
{
static LRESULT CALLBACK callback(HWND wnd, UINT msg, WPARAM wparam,
LPARAM lparam)
{
Dialog<Impl> * dlg =
reinterpret_cast<Dialog<Impl>*>(GetWindowLong(wnd, GWL_USERDATA));

switch (msg)
{
case WM_INITDIALOG:
dlg = reinterpret_cast<Dialog<Impl>*>(lparam);
SetWindowLong(wnd, GWL_USERDATA, reinterpret_cast<LONG>(dlg));
dlg->dlg = wnd;
return dlg->init();
case WM_COMMAND:
{
if (dlg == 0)
{
return 0;
}
UINT control = LOWORD(wparam);
UINT message = HIWORD(wparam);
return dlg->commandEvent(control, message);
}
default:
if (dlg == 0)
{
return 0;
}
return dlg->processUnknown(msg, wparam, lparam);
}
return 0;
}

protected:
HWND dlg;

bool applied;

virtual LRESULT init() = 0;
virtual LRESULT commandEvent(UINT control, UINT message) = 0;

virtual LRESULT processUnknown(UINT msg, WPARAM wparam, LPARAM
lparam) { return 0; }

Dialog() : dlg(0), applied(false) {}

public:
bool show(HINSTANCE inst, HWND parent)
{
DialogBoxParam(inst, MAKEINTRESOURCE(Impl::DIALOG_ID), parent,
reinterpret_cast<DLGPROC>(&Dialog<Impl>::callback),
reinterpret_cast<LPARAM>(this));
//ShowDialogBox(parent,
reinterpret_cast<DLGPROC>(&FSDialog<Impl>::callback), Impl::DIALOG_ID,
reinterpret_cast<LPARAM>(this));
return applied;
}
HWND dialogWindowHandle() const { return dlg; }
};

Note that this is an instance of the curriously reaccuring template
pattern that requires, among other things, a DIALOG_ID in the class it
is inherited by and instantiated with.

That is but one way to introduce OO into C type gui libraries like
win32. There is also the lookup table approach. The static function
has a static local lookup table filled with window pointer to window
handler class instance values. A lot of people don't like the approach
above but it works.
 
D

dasjotre

Noah said:
You can't do what you are trying to do. For one thing there is no way
to access a in dlgTest. For another thing, and this breaks away from
C++ proper, is that your dlgproc is not correctly declared or used. An
alternative approach is something like this:

template <typename Impl>
class Dialog
{
static LRESULT CALLBACK callback(HWND wnd, UINT msg, WPARAM wparam,
LPARAM lparam)
{
Dialog<Impl> * dlg =
reinterpret_cast<Dialog<Impl>*>(GetWindowLong(wnd, GWL_USERDATA));

switch (msg)
{
case WM_INITDIALOG:
dlg = reinterpret_cast<Dialog<Impl>*>(lparam);
SetWindowLong(wnd, GWL_USERDATA, reinterpret_cast<LONG>(dlg));
dlg->dlg = wnd;
return dlg->init();
case WM_COMMAND:
{
if (dlg == 0)
{
return 0;
}
UINT control = LOWORD(wparam);
UINT message = HIWORD(wparam);
return dlg->commandEvent(control, message);
}
default:
if (dlg == 0)
{
return 0;
}
return dlg->processUnknown(msg, wparam, lparam);
}
return 0;
}

protected:
HWND dlg;

bool applied;

virtual LRESULT init() = 0;
virtual LRESULT commandEvent(UINT control, UINT message) = 0;

virtual LRESULT processUnknown(UINT msg, WPARAM wparam, LPARAM
lparam) { return 0; }

Dialog() : dlg(0), applied(false) {}

public:
bool show(HINSTANCE inst, HWND parent)
{
DialogBoxParam(inst, MAKEINTRESOURCE(Impl::DIALOG_ID), parent,
reinterpret_cast<DLGPROC>(&Dialog<Impl>::callback),
reinterpret_cast<LPARAM>(this));
//ShowDialogBox(parent,
reinterpret_cast<DLGPROC>(&FSDialog<Impl>::callback), Impl::DIALOG_ID,
reinterpret_cast<LPARAM>(this));
return applied;
}
HWND dialogWindowHandle() const { return dlg; }
};

Note that this is an instance of the curriously reaccuring template
pattern that requires, among other things, a DIALOG_ID in the class it
is inherited by and instantiated with.

That is but one way to introduce OO into C type gui libraries like
win32. There is also the lookup table approach. The static function
has a static local lookup table filled with window pointer to window
handler class instance values. A lot of people don't like the approach
above but it works.

ATL windowing uses stack hacking they,
in a typical MSoft way of redefining
common terms, call 'thunking'.
It changes the first argument of the window
procedure (HWND) with a pointer to the
class that handles the calls. I believe MFC
does something similar, but I'm not sure.

Check atlstdthunk.h

Its hacky but fast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top