473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

static class member - access to variables

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.

Nov 16 '06 #1
3 2541

tom wrote:
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

Nov 16 '06 #2

tom wrote:
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>::callbac k), 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.

Nov 16 '06 #3

Noah Roberts wrote:
tom wrote:
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>::callbac k), 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.

Nov 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
5
by: Tom Pearson | last post by:
What is the scope of static variable when programming in ASP.NET? For example I have a control class that uses static callbacks so that another window can pass a list of items to it. The control...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.