开发者

Translating C++ Classes to Delphi

开发者 https://www.devze.com 2023-01-30 14:27 出处:网络
I am translating some C++ code to Delphi and there are some abstract classes that need to be translated. These classes are used as parameter/return types, etc, and my question is if a C++ class hierar

I am translating some C++ code to Delphi and there are some abstract classes that need to be translated. These classes are used as parameter/return types, etc, and my question is if a C++ class hierarchy such as this:

class Thing {
    virtual void blah() = 0;
};

class Thing2 : public Thing {
    virtual bool asdf(Thing*) = 0;
};

can be rewritten in Delphi as:

Thing = class
    开发者_高级运维procedure blah; virtual;
end;

Thing2 = class(Thing)
    function asdf(Thing) : Boolean; virtual;
end;

And the Delphi code can call C++ functions that take C++ Thing*s and stuff, and C++ code can call Delphi functions that take Delphi Things, etc. So basically, if the above translation is made, will a C++ Thing2* equal a Delphi Thing2 where Delphi can call it's methods, etc?


Not quite. In C++, marking a method as = 0 means it's an abstract method. In Delphi, to get the same effect, you have to mark it as virtual; abstract;, not just as virtual;.

Also, in Delphi, if you place a class member declaration immediately under the class name, it'll be declared by default as published, which means it's public plus RTTI is generated for it. That's probably not your intention, so put a visibility scope declaration (private, protected or public) first:

Thing = class
public
    procedure blah; virtual; abstract;
end;

Thing2 = class(Thing)
public
    function asdf(Thing) : Boolean; virtual; abstract;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消