static class member - access to variables

Discussion in 'C++' started by tom, Nov 16, 2006.

  1. tom

    tom Guest

    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.
     
    tom, Nov 16, 2006
    #1
    1. Advertising

  2. tom

    dasjotre Guest

    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
     
    dasjotre, Nov 16, 2006
    #2
    1. Advertising

  3. tom

    Noah Roberts Guest

    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>::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.
     
    Noah Roberts, Nov 16, 2006
    #3
  4. tom

    dasjotre Guest

    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>::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.
     
    dasjotre, Nov 17, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,424
    Tony Morris
    Oct 19, 2004
  2. E11
    Replies:
    1
    Views:
    4,800
    Thomas Weidenfeller
    Oct 12, 2005
  3. Markus Dehmann
    Replies:
    5
    Views:
    2,692
    red floyd
    Jul 1, 2004
  4. Siemel Naran
    Replies:
    4
    Views:
    810
    Micah Cowan
    Jan 12, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,354
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page