翻译 C++德尔福课程

发布于 2024-10-06 19:09:28 字数 551 浏览 3 评论 0原文

我正在将一些 C++ 代码翻译为 Delphi,并且有一些抽象类需要翻译。这些类用作参数/返回类型等,我的问题是,像这样的 C++ 类层次结构是否

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

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

可以在 Delphi 中重写为:

Thing = class
    procedure blah; virtual;
end;

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

Delphi 代码可以调用采用 C++ Thing*s 和东西的 C++ 函数,并且C++ 代码可以调用采用 Delphi Things 等的 Delphi 函数。所以基本上,如果进行上述翻译,C++ Thing2* 是否等于 Delphi Thing2,其中 Delphi 可以调用它的方法等?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-10-13 19:09:28

不完全是。在 C++ 中,将方法标记为 = 0 表示它是抽象方法。在Delphi中,要获得相同的效果,必须将其标记为virtual;抽象;,而不仅仅是虚拟;。

另外,在 Delphi 中,如果您将类成员声明直接放在类名下,则默认情况下它将被声明为 published,这意味着它是公共的,并且会为其生成 RTTI。这可能不是您的意图,因此首先放置可见性范围声明(private、protectedpublic):

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

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文