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

B

Barry

Neviton said:
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.
 
N

Neviton

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
 
B

Barry

Neviton said:
Thanks for the quickly response Barry.

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.
 
P

Puppet_Sock

Thanks for the quickly response Barry.


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
 
N

Neviton

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
 
S

Scott McPhillips [MVP]

Neviton said:
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).
 
P

Puppet_Sock

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
 
N

Neviton

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()
{
}
 
A

Alf P. Steinbach

* 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
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top