Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I access non-static variables from static funcition?

Reply
Thread Tools

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

 
 
Neviton
Guest
Posts: n/a
 
      09-17-2007
Is it possible ?

There is any work around?

Thanx

 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-17-2007
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
 
Reply With Quote
 
 
 
 
Neviton
Guest
Posts: n/a
 
      09-17-2007
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


 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      09-17-2007
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
 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      09-17-2007
On Sep 17, 8:47 am, Neviton <(E-Mail Removed)> 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

 
Reply With Quote
 
Neviton
Guest
Posts: n/a
 
      09-17-2007
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

 
Reply With Quote
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      09-17-2007
"Neviton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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).

 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      09-17-2007
On Sep 17, 12:07 pm, Neviton <(E-Mail Removed)> 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

 
Reply With Quote
 
Neviton
Guest
Posts: n/a
 
      09-17-2007
> 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()
{
}

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-17-2007
* 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Static inline functions with static local variables Martin Wells C Programming 10 10-08-2007 03:38 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Can't access static member var from static method. Why? Markus Dehmann C++ 5 07-01-2004 04:16 PM



Advertisments