We help IT Professionals succeed at work.

static method question

Dmitry_
Dmitry_ asked
on
Hello,
I have a function:
class function CalcIntervalsPerHr(AIntervals: Intervals): integer;
When I'm trying to use class containing this function on the C++ Builder, the C++ Builder is generating the following code in the hpp file:
static int __fastcall CalcIntervalsPerHr(TMetaClass* vmt, Jzcwcommon::TIntervals AIntervals);
What is the vmt paramether and what value should I pass to this parameter?
Comment
Watch Question

Commented:
Hi,

your 'function' is in fact a class method.

the vmt parameter is the implicit class reference.

Delphi equivalent is the hidden Self parameter (passed on the stack of every method) which in case of normal methods points to the object's instance, and in case of class methods points to the class reference (because there may be no instance; class functions can be called without an instance).

This is hidden in Delphi but in CBuilder it's obviously not. I guess you should pass the class reference to the vmt parameter because that's how it works in Delphi behind the scenes.

Maybe you don't even have to typecast, e.g.

CalcIntervalsPerHr(TMyObject, SomeIntervals);

perhaps you have to typecast, e.g.

CalcIntervalsPerHr((TMetaClass*) TMyObject, SomeIntervals);

where TMyObject is your class where you declared the CalcIntervalsPerHr method.

Something like this - I'm not sure; my experience with CBuilder has been very brief.

HTH
TOndrej

Author

Commented:
Hi,
this is a _static_ class function
The static class functions haven't Self paramether and can't access object's instance.
Dima

Commented:
Hi Dmitry,

I was talking about Delphi.
For class methods, Delphi uses 'Self' as the class reference. According to your posting, CBuilder uses 'vmt'.
In Delphi 'Self' is implicit and hidden (not used in the declaration). In CBuilder, it's not.
I believe they are the same, although the names are different.

'static function' used in a CBuilder class declaration = 'class method' in Delphi.

In case it's not clear, here's a recap:

method
- can only be invoked on an instance
- first parameter (EAX) receives the instance pointer

class method
- can be invoked without an instance
- first parameter (EAX) receives the class reference

Therefore, you need to pass the class reference to the vmt parameter, that's all.
How to do that, I'm not sure. I haven't used CBuilder much.
I gave you some tips already.

HTH
TOndrej
Commented:
Hi, I've just seen this on Borland newsgroups, perhaps this will do it:

__classid(TMyClass)

Commented:
Why B? :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.