Advertisement

Function pointers in borland.

Started by October 17, 2000 07:31 PM
8 comments, last by JwayneT 24 years, 3 months ago
Hey,

I''ve set up a base class with function pointers as member variables.
class BC { protected: void (*Fnc)(void*); void (*Fnc2)(void*); //and so on };
But when I do this
class DC : public BC { public: DC(){Fnc=Some_function_defined_earlier;...//and so on}; }
I get the error ''Member function must be called or it''s address taken''. Now another member of my team used the same syntax but in microsoft C++ and it worked fine. Doing typedef statements before the class doesn''t work either, what''s missing? a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

-=CF=-
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
I've done it like this in one of my custom controls.

                typedef void __fastcall (__closure *TPointEvent)(TObject *Sender, TEnvelopePoint *Point);class PACKAGE TEnvelope : public TCustomControl{private:  // ...snip...  TPointEvent FOnMovePoint;  TPointEvent FOnAddPoint;  TPointEvent FOnDeletePoint;  TPointEvent FOnSelectPoint;  TPointEvent FOnDeselectPoint;  // ...snip...}  


This implements events in a control. To use pointers to standard functions, remove the __closure from the typedef. The __closure directive instructs BC++ to allow for an object instance pointer plus a function pointer because a method pointer requires both an object instance and a function.


Steve 'Sly' Williams
Tools Developer
Krome Studios

Edited by - Sly on October 19, 2000 2:31:21 AM
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
Advertisement
Also __closure allows inherited virtual functions to be called correctly, so you can have a function pointer to something in a class, that if derived / overrided can be called correctly.
AND, if you use the IDE to publish properties, the __closures will automatically be written out / read in to your DFM (form) file. Which means no extra work.

------------------------------
BCB DX Library - RAD C++ Game development for BCB
Much thankx guys.
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
Another thing to consider is that Borland is more ANSI compliant. If you''re assigning the address of a member function, like this:

class DC: public BC {
public:
void thefunc(void *ptr);
DC() {
Fnc=thefunc;
}
};

Then you must define Fnc as:

class BC {
void (*BC::Fnc)(void*);
};

Cheers,
David

Another thing to consider is that Borland is more ANSI compliant. If you''re assigning the address of a member function, like this:

class DC: public BC {
public:
void thefunc(void *ptr);
DC() {
Fnc=thefunc;
}
};

Then you must define Fnc as:

class BC {
void (*BC::Fnc)(void*);
};

Cheers,
David

Advertisement
ANSI requires you to use the address-of operator e.g.:

pMethod = &SomeMethod

VC will let you get away without:

pMethod = SomeMethod; // Works in VC, not ANSI

I hit this all the time going from VC to gcc.

-Mike
Yeah,

I''m a bigger fan of Borland because it''s more ANSI compliant. Thankx for all of the info, it''s going to help alot in the future. MSVC++ should acept all of this if this other guy needs to compile on his machine right?

a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

-=CF=-
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
Dolt!
Don't use the wonderful __closure with MSVC, cos MSVC doesn't implement it. Otherwise, yes, go ahead and use function pointers (but more strict ANSI to compile under Borland).
Also, if you want to compile w/ MSVC, try to use STL container classes (all compilers on all platforms implement these well), and *unfortunely* you will have to stay away from things like components, event handlers, properties, etc.


------------------------------
BCB DX Library - RAD C++ Game development for BCB

Edited by - c++freak on October 19, 2000 8:46:37 PM
JwayneT -- yes, the code posted here will work in Microsoft as well.



David

This topic is closed to new replies.

Advertisement