How can I access non-static variables from static funcition?

Discussion in 'C++' started by Neviton, Sep 17, 2007.

  1. Neviton

    Neviton Guest

    Is it possible ?

    There is any work around?

    Thanx
    Neviton, Sep 17, 2007
    #1
    1. Advertising

  2. Neviton

    Barry Guest

    Neviton wrote:
    > Is it possible ?
    >
    > There is any work around?
    >
    > Thanx
    >


    If you need to modify an instance, pass in an reference or pointer to
    the object, if you just want to call the function, then create a temp
    obj (const/non-const).
    Last resort, there must be something wrong with your design, refactor.


    --
    Thanks
    Barry
    Barry, Sep 17, 2007
    #2
    1. Advertising

  3. Neviton

    Neviton Guest

    Thanks for the quickly response Barry.

    > Last resort, there must be something wrong with your design, refactor.

    Yes probably. :) I'm starting.
    Perhaps you can give me some tip.

    I'm subclassing a Button control.
    I create a class for this and I define my subclassing procedure as
    static.
    But I have a non-static HBITMAP variable that I want to use inner
    subclassing static procedure.

    thanks again
    Neviton, Sep 17, 2007
    #3
  4. Neviton

    Barry Guest

    Neviton wrote:
    > Thanks for the quickly response Barry.
    >
    >> Last resort, there must be something wrong with your design, refactor.

    > Yes probably. :) I'm starting.
    > Perhaps you can give me some tip.
    >
    > I'm subclassing a Button control.
    > I create a class for this and I define my subclassing procedure as
    > static.
    > But I have a non-static HBITMAP variable that I want to use inner
    > subclassing static procedure.
    >


    In this case I would rather make the member function non-static,
    if you can't make it non-static, then the former two ways can solve the
    problem.

    --
    Thanks
    Barry
    Barry, Sep 17, 2007
    #4
  5. Neviton

    Puppet_Sock Guest

    On Sep 17, 8:47 am, Neviton <> wrote:
    > Thanks for the quickly response Barry.
    >
    > > Last resort, there must be something wrong with your design, refactor.

    >
    > Yes probably. :) I'm starting.
    > Perhaps you can give me some tip.
    >
    > I'm subclassing a Button control.
    > I create a class for this and I define my subclassing procedure as
    > static.
    > But I have a non-static HBITMAP variable that I want to use inner
    > subclassing static procedure.


    Presumably you are doing this because there is some
    library code (the operating system, etc.) that looks
    after the mundane parts of doing the button operations.
    And that other code wants a static function so it can
    be used as a callback. This is an evergreen question
    that gets asked repeatedly. Read through the FAQ for
    what to do about callbacks.

    But basically: A static member function does not know
    about specific instances of the class. You've got to
    tell it somehow. It isn't really possible for me to
    solve your problem without actually doing all the work.
    You need to have some scheme for keeping track of which
    button-class instance is being invoked. Some callback
    systems allow the setting of a parameter that can be
    a pointer. Some allow the storage of some kind of ID
    number that your prog could keep track of buttons.
    Or possibly there is some ID number the system keeps
    track of automatically and you can use that. In window
    systems there is usually a window ID or a frame number
    or some such.

    So, when you are setting up the button, you'd store
    something in the system. When the callback happens,
    it passes you some extra parameter. And you use that
    extra parameter to sort out which button instance
    you need to call. Your static function looks up this
    stuff in a static table, then calls it. That table
    is something you have to maintain as buttons are
    created, destroyed, modified, etc.

    If at all possible, it's way better to have the
    callback system get a pointer to an instance. Then
    if you can persuade it to call a member function
    to do the job you can shorten this entire problem.
    But that assumes you have the ability to modify
    the system so that works. If you don't, then you
    have to do something like the static table.
    Socks
    Puppet_Sock, Sep 17, 2007
    #5
  6. Neviton

    Neviton Guest

    I tried to change my function to non-static but I get this error:

    ....Button.cpp aggregate value used where an integer was expected

    this occur on this line when I try get the address of my subclass
    procedure(OwnerDrawButtonProc):

    mainWindowProc = (WNDPROC) SetWindowLong(handle, GWL_WNDPROC, (LONG)
    OwnerDrawButtonProc);

    Any help is welcome

    And

    Thanks for your reply Puppet_Sock
    Neviton, Sep 17, 2007
    #6
  7. "Neviton" <> wrote in message
    news:...
    >
    > Any help is welcome


    You are attempting to reinvent the wheel. Microsoft provides at least 3 C++
    libraries that solve this problem. (ATL, WTL, MFC).
    Scott McPhillips [MVP], Sep 17, 2007
    #7
  8. Neviton

    Puppet_Sock Guest

    On Sep 17, 12:07 pm, Neviton <> wrote:
    > I tried to change my function to non-static but I get this error:
    >
    > ...Button.cpp aggregate value used where an integer was expected
    >
    > this occur on this line when I try get the address of my subclass
    > procedure(OwnerDrawButtonProc):
    >
    > mainWindowProc = (WNDPROC) SetWindowLong(handle, GWL_WNDPROC, (LONG)
    > OwnerDrawButtonProc);
    >
    > Any help is welcome


    You have moved into the area of specifics of a libary for
    doing window stuff. This is outside of what happens in this
    news group as we are only interested in standard C++ language
    stuff here. You need to go to a group that is specific to
    your compiler and operating system.

    It *looks* like Microsoft, but I'm not sure. If it is, you
    need a news group with Microsoft or Visual C++ or DOS or
    something like that in the name. Go to groups.google.com
    and search around for groups that talk about your compiler
    and operating system.

    Basically, if I'm recalling those things correctly, that
    function wants the address of a static function. It tells
    the operating system to call that function whenever it
    needs to draw the button. It can not be a class non-static
    member function. It has to be static. Then you have to
    re-read the post I posted before. Then go to that other
    news group.
    Socks
    Puppet_Sock, Sep 17, 2007
    #8
  9. Neviton

    Neviton Guest

    > You have moved into the area of specifics of a libary for
    > doing window stuff. This is outside of what happens in this
    > news group as we are only interested in standard C++ language
    > stuff here. You need to go to a group that is specific to
    > your compiler and operating system.


    Yes, I'm playing with WIN32 stuff but the error don't have anything to
    do with this.
    I create one class just to make sure that this error occur even
    without WIN32 stuff.

    Well
    I first wanna to say thanks for all your patience
    because what I know about C++ is so little.
    And
    Take a look on this code that rise the same error( no Win32 stuff
    now :] ):
    ....MyClass.cpp In member function `void MyClass::Initialize()':
    ....MyClass.cpp aggregate value used where an integer was expected
    ....Makefile.win [Build Error] [MyClass.o] Error 1

    The code:


    /* MyClass.h *****************/
    class MyClass
    {
    public:
    void Initialize();
    void MyFunction();
    };

    /* MyClass.cpp **************/
    #include "MyClass.h"
    void MyClass::Initialize()
    {
    long address = (long)MyFunction; //this line rises the error
    }

    void MyClass::MyFunction()
    {
    }
    Neviton, Sep 17, 2007
    #9
  10. * Puppet_Sock:
    >
    > So, when you are setting up the button, you'd store
    > something in the system. When the callback happens,
    > it passes you some extra parameter. And you use that
    > extra parameter to sort out which button instance
    > you need to call. Your static function looks up this
    > stuff in a static table, then calls it. That table
    > is something you have to maintain as buttons are
    > created, destroyed, modified, etc.


    My favorite mechanism at one time was instead one used in Borland's
    windowing framework, whatever it was called (ObjectWindows?). Instead
    of using a table, or storing a pointer to the C++ object in the API
    level object, one generated a thunk on the fly to use as callback, said
    thunk -- of machine code -- just calling a common callback with a
    pointer to the C++ object as argument. Of course, today, in Windows I
    guess the nearest antivirus software, or Vista's paranoid protection,
    would stop the self-modifying code immediately and cry wolf! wolf! woof!

    Q. What's wrong, my machine slows down to a snail's pace and there are
    funny black artifacts on the screen and the disk is very very active?
    A. You have anti-virus.

    Cheers,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Sep 17, 2007
    #10
    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. natman

    help with a funcition-please

    natman, Jun 5, 2006, in forum: C Programming
    Replies:
    11
    Views:
    426
    Simon Biber
    Jun 7, 2006
  2. Replies:
    4
    Views:
    1,075
    Richard Tobin
    Dec 12, 2006
  3. Alan Woodland
    Replies:
    12
    Views:
    539
    Marco Manfredini
    Nov 22, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,342
    Pete Becker
    Dec 5, 2007
  5. rickman
    Replies:
    5
    Views:
    427
    rickman
    Mar 30, 2013
Loading...

Share This Page