Objective-c 运行时意味着比直接 c/c++ 多了一层程序?

发布于 2024-12-03 21:30:18 字数 359 浏览 2 评论 0原文

我读过,objective-c 程序需要 Objective-c 运行时才能运行。

AFAIK,这两个 C/C++ 程序都不需要任何运行时环境来运行。因为生成的二进制代码是由底层操作系统直接执行的。

那么这意味着 Objective-c 程序需要冗余层才能运行,对吗?如果是这样,这一层是否看起来像 Java VM 和 .net 运行时,或者看起来像 Qt 运行时(具有一些附加库的风格)?

编辑: 经过一番阅读,我发现 objc 编译器在生成的编译代码中生成了更多信息,这些信息负责许多事情,例如方法传递(objc_sendMsg()内省和其他)

谢谢。

I've read that, the objective-c programs need objective-c runtime to run.

AFAIK, both C/C++ programs don't require any runtime environments to run. as the generated binary code is being executed directly by the underlying OS.

So this means that Objective-c programs require a redundant layer to run, correct? and If so, is this layer seems like Java VM and .net runtime or seems like Qt runtime (in flavor of some additional libraries)?

EDIT:
After some read, I found that, the objc compiler generates some more information in the generated compiled code that is responsible of many things such as method passing (objc_sendMsg(), introspection and others)

Thanks.

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

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

发布评论

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

评论(1

春庭雪 2024-12-10 21:30:18

编译后的代码是本机的,但您需要一个额外的库(运行时)来执行所有对象和消息处理(查找、调用等)。不涉及虚拟机。所以它更像是QT而不是Java运行时。

[更新]

由于 C++ 消息绑定行为对于更动态的 OO 语言(例如:Objective-C 或 Smalltalk)的程序员来说并不明显 - 像我一样 - 我编写了一个小型 C++ 测试应用程序来演示效果virtual 关键字决定了要调用的方法的选择。

#include <iostream>

class Test1 {
public:
    Test1();
    void test1();
    void test2();
};

class Test2 : Test1 {
public:
    Test2();
    void test1();
    void test2();
};

Test1::Test1() {}
void Test1::test1() { std::cout << "T1:t1" << std::endl; }
void Test1::test2() { std::cout << "T1:t2" << std::endl; }

Test2::Test2() {}
void Test2::test1() { std::cout << "T2:t1" << std::endl; }
void Test2::test2() { std::cout << "T2:t2" << std::endl; }

int main(int argc, char **argv)
{
    Test1 *t11 = new Test1();
    Test1 *t12 = (Test1 *)(new Test2());
    Test2 *t2 = new Test2();

    t11->test1();
    t11->test2();

    t12->test1();
    t12->test2();

    t2->test1();
    t2->test2();

    return 0;
}

Objective-C 程序员期望输出为 ,

T1:t1
T1:t2
T2:t1
T2:t2
T2:t1
T2:t2

因为 t12 实际上是一个被转换为 Test1Test2
实际输出是

T1:t1
T1:t2
T1:t1
T2:t2
T2:t1
T2:t2

因为 C++(默认情况下,即没有 virtual)根据编译时知道的类型(即 Test1<)静态绑定对 test1 的调用。 /code> (由于演员阵容)。

The compiled code is native but you need an additional library (the runtime) which does all the object and message handling (lookup, invocation etc.). There is no virtual machine involved. So it is more like QT than Java runtime.

[Update]

Since the C++ message binding behaviour is not obvious to programmers of more dynamic OO languages (e.g.: Objective-C or Smalltalk) - like me - I wrote a small C++ test app which demonstrates the effect of the virtual keyword on the choice of the method to call.

#include <iostream>

class Test1 {
public:
    Test1();
    void test1();
    void test2();
};

class Test2 : Test1 {
public:
    Test2();
    void test1();
    void test2();
};

Test1::Test1() {}
void Test1::test1() { std::cout << "T1:t1" << std::endl; }
void Test1::test2() { std::cout << "T1:t2" << std::endl; }

Test2::Test2() {}
void Test2::test1() { std::cout << "T2:t1" << std::endl; }
void Test2::test2() { std::cout << "T2:t2" << std::endl; }

int main(int argc, char **argv)
{
    Test1 *t11 = new Test1();
    Test1 *t12 = (Test1 *)(new Test2());
    Test2 *t2 = new Test2();

    t11->test1();
    t11->test2();

    t12->test1();
    t12->test2();

    t2->test1();
    t2->test2();

    return 0;
}

An Objective-C programmer would expect a output of

T1:t1
T1:t2
T2:t1
T2:t2
T2:t1
T2:t2

since t12 is actually a Test2 which was casted to Test1.
The actual output is

T1:t1
T1:t2
T1:t1
T2:t2
T2:t1
T2:t2

because C++ (by default, i.e. without virtual) statically binds the call to test1 based on the type it knows at compile time which is Test1 (due to the cast).

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