469,889 Members | 1,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

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

Is it possible ?

There is any work around?

Thanx

Sep 17 '07 #1
8 1679
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
Sep 17 '07 #2
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
Sep 17 '07 #3
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
Sep 17 '07 #4
On Sep 17, 8:47 am, Neviton <nevito...@gmail.comwrote:
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

Sep 17 '07 #5
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

Sep 17 '07 #6
"Neviton" <ne*******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
>
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).

Sep 17 '07 #7
On Sep 17, 12:07 pm, Neviton <nevito...@gmail.comwrote:
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

Sep 17 '07 #8
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()
{
}

Sep 17 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

92 posts views Thread by Jeffrey P via AccessMonster.com | last post: by
6 posts views Thread by Woody Splawn | last post: by
17 posts views Thread by Mell via AccessMonster.com | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.