 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
phuknutt Guest
|
Posted: Sun Jul 27, 2003 7:33 am Post subject: Calling TForm functions from another class? |
|
|
I have 2 classes, the 1st is a regular TForm-derived class 'MyForm'
which contains all the application buttons, forms, etc. The second
class is a plain user-made class which determines when MyForm needs to
be updated. The problem is, since I can't directly call functions in
MyForm from the user-made class, I figured the bast way to handle this
would be to create a virtual base class and have MyForm devived from
it along with TForm. That way I can call the base function to access
the TForm's functions. As you may know, it's an error to have a
TForm-derived class also derive from another class. Is there an
alternative way to handle this problem? I've tried using a template
class with function pointers - which turned out to be a disaster.
|
|
| Back to top |
|
 |
Greg Bryant Guest
|
Posted: Mon Jul 28, 2003 7:10 pm Post subject: Re: Calling TForm functions from another class? |
|
|
[email]phuknuttvi (AT) hotmail (DOT) com[/email] (phuknutt) wrote in
news:81202b5f.0307262333.7c3a6132 (AT) posting (DOT) google.com:
| Quote: | I have 2 classes, the 1st is a regular TForm-derived class 'MyForm'
which contains all the application buttons, forms, etc. The second
class is a plain user-made class which determines when MyForm needs to
be updated. The problem is, since I can't directly call functions in
MyForm from the user-made class, I figured the bast way to handle this
would be to create a virtual base class and have MyForm devived from
it along with TForm. That way I can call the base function to access
the TForm's functions. As you may know, it's an error to have a
TForm-derived class also derive from another class. Is there an
alternative way to handle this problem? I've tried using a template
class with function pointers - which turned out to be a disaster.
|
Funny, I've been puzzling that one, too. Accessor functions or friend
classes. Accessors seem so overhead-y (particularly when hammering a
database on the main form), and friend functions seem so c-ish. Right
now I'm doing both. So other forms/classes can call functions to set
columns visible or not, trigger a database rebuild, etc., but if they're
diving hard into the database, I might just make them a friend.
A lot of what I have now is just the result of zero design. Everything
in the app I'm playing with now is just grown rather than planned, so I
don't have a good separation of model/view. Too often the event handler
does the action, rather than accessing a decent class to do the action
itself. Somehow I lost the distinction between visual and non-visual
classes, and I'm too lazy (and nobody's paying me for this!) to go back
and do it right.
If you have a good design (i.e. your main form really is just the view,
and your "plain user-made class" is more in the way of controlling how
the view changes) I'd just set up accessor functions.
|
|
| Back to top |
|
 |
JKroetch Guest
|
Posted: Wed Aug 13, 2003 4:18 pm Post subject: Re: Calling TForm functions from another class? |
|
|
[email]phuknuttvi (AT) hotmail (DOT) com[/email] (phuknutt) wrote in message news:<81202b5f.0307290133.16944092 (AT) posting (DOT) google.com>...
| Quote: |
Friend functions are out of the question because in the final design
the "plain user-made class" will be hard coded and will need to have
access to the developer's own TForm-derived class. So I cant just say
'MyForm' is a friend. It seems accessor functions might have the same
restrictions.
|
Actually, I believe he meant make a friend class in 'MyForm' that
handles incoming/outgoing events (and that friend class will relay the
message to the MyForm).
example: (this is more pseudo-code than code, so take it with a grain
of salt)
////////////////////////////////////////////////////////
class TMyForm : public TForm
{
__published: // yadda yadda
private:
// Friend
class TMyFormObserver;
friend class TMyFormObserver;
// Implementation
class TMyFormObserver : public IMyObserverInterface
{
private:
TMyForm* m_pParent;
TMyModel* m_pModel;
public:
TMyFormObserver(TMyForm* pParent, TMyModel* pModel);
virtual ~TMyFormObserver();
// My Observer/Interface/Virtual functions here
void DoSomething();
}
};
////////////////////////////////////////////////////////
TMyForm would create it's friend class observer and that observer
would register itself with the Model you want to receive events from.
When the observer gets the event, it calls the appropriate function in
the main form.
This is the way I've implemented it in my code to get around the fact
that a builder VCL class cannot inherit from more than one class (even
a damn abstract/interface class).
Hope this helps!
John
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|