Thread

aw_event_set() and Form Classes (Sdk)

aw_event_set() and Form Classes // Sdk

1  |  

percipient

Aug 26, 2002, 6:23pm
I got my previous question about converting co-ords all worked out, thanks
to everyone;s help and lots of trial and error. Now I have another beginner
question. I'm using Borland C++ Builder 6.

Is there a way to get the handler for an event to point to a function that's
part of a form, as in leading to...
void TMainWindow::AW_AvatarAdd(void)
instead of one outside of the form's class, like
void AW_AvatarAdd(void)
?

If I point an event to a handler that's part of the form, I get the
following errors on compile...
[C++ Error] Main.cpp(110): E2034 Cannot convert 'void (* (_closure )())()'
to 'void (*)()'
[C++ Error] Main.cpp(110): E2342 Type mismatch in parameter 'handler'
(wanted 'void (*)()', got 'void')

It's not too terrible having it outside, but then it does not show up in
ClassExplorer and I have to put MainWindow-> in front of most things. I'd
rather not if I don;t have to.

Thanks for your help,
PercipientI got my previous question about converting co-ords all worked
out, thanks to everyone;s help and lots of trial and error. Now I have
another beginner question. I'm using Borland C++ Builder 6.

Is there a way to get the handler for an event to point to a function that's
part of a form, as in leading to...
void TMainWindow::AW_AvatarAdd(void)
instead of one outside of the form's class, like
void AW_AvatarAdd(void)
?

If I point an event to a handler that's part of the form, I get the
following errors on compile...
[C++ Error] Main.cpp(110): E2034 Cannot convert 'void (* (_closure )())()'
to 'void (*)()'
[C++ Error] Main.cpp(110): E2342 Type mismatch in parameter 'handler'
(wanted 'void (*)()', got 'void')

It's not too terrible having it outside, but then it does not show up in
ClassExplorer and I have to put MainWindow-> in front of most things. I'd
rather not if I don;t have to.

Thanks for your help,
Percipient

deconstructor

Aug 27, 2002, 1:07pm
You have to make the member function static:

class TMainWindow{
//yadda yadda
static void AW_AvatarAdd(void);
};

void TMainWindow::AwAvatarAdd(void){
//foobar
}

Why? The event handler triggers the function, however, the function can be
part of a class--and there can be 1000's of TMainWindows, so the program has
no way of determining where to trigger the function. The static keyword
makes that member common to all instances of the class--for example:

class foo {
public:
static int bar;
};
foo class1, class2;
class1.bar=1; // class2.bar = 1 now also--you can also do foo::bar

This method should have no problem, however, seeing as you have only one
window and will only have one bot.

Object oriented power to the max!

Deconstructor

[View Quote]

deconstructor

Aug 27, 2002, 1:17pm
Just a side note--the only problem I see that you will probably encounter is
the fact that static members can NOT read or write to nonstatic members
(however, vice versa works). Possible solutions:

One solution is to make any variables you require to use in the event
handlers static, which is ok if there are only a few. If you have ot make
the majority of the members in the class static so you can access them,
what's the point in having the class?

Another solution is to have a pointer somewhere to your TMainWindow class in
your application, so you can access it from your event handler and get out
of the static function.

What I've been doing in my object oriented AWBot class is it has static
member classes of AWBotInstance--AWBot class has the static members
AwEventAvatarAdd, etc. which get called when the events happen. These
member functions check aw_instance against every AWBotInstance->instance
until they find the match, and then they call
AWBotInstance->AwEventAvatarAdd. Works pretty well, actually. :)

Deconstructor

1  |  
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2024. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn