简体   繁体   English

面向对象的C编程-等同于“ this”关键字?

[英]Object oriented C programming - equivalent of 'this' keyword?

I'm experimenting with OOP in C, based off of this answer. 基于答案,我正在C中进行OOP实验。 I came across something I can't quite get around. 我遇到了无法完全解决的问题。 Take this example: 举个例子:

struct foo {
    int val;
    int (*bar)(struct foo, int);
}

int foo_bar(struct foo mod, int val)
{
    mod.val = val;
}

int main(void)
{
    struct foo foo;
    foo.bar = foo_bar;
    foo.bar(foo, 8);
}

I think it would be much simpler and clearer if there was a way to use the this keyword in C: 我认为,如果有一种方法可以在C中使用this关键字,它将更加简单明了:

struct foo {
    int val;
    int (*bar)(struct foo, int);
}

int foo_bar(int val)
{
    this.val = val;
}

int main(void)
{
    struct foo foo;
    foo.bar = foo_bar;
    foo.bar(8);
}

It sounds impossible, but there may be some workaround out there, a bit like OOP in C itself. 听起来是不可能的,但是可能有一些解决方法,有点像C本身的OOP。 Is there any way to achieve the functionality of the this keyword in Object-Oriented C? 有什么方法可以在面向对象的C语言中实现this关键字的功能吗?

No. this keyword in C++ is a reference to the object at hand, and is actually explicitly passed to the member functions at the ABI level. 不可以。C++中的this关键字是对当前对象的引用,实际上是在ABI级别上显式传递给成员函数的。 Explicitly passing a pointer to the object (as the first parameter) in functions is the best equivalent in C. Note that this means 在函数中显式传递指向对象的指针(作为第一个参数)是C语言中的最佳等效项。请注意,这意味着

struct foo {
    int   value;
    int (*func)(struct foo *, int);
};

void foo_bar(struct foo *f, int value)
{
    f->value = value;
}

ie the pointer to the object is passed as the first parameter, rather than the structure itself. 即, 指向对象的指针作为第一个参数而不是结构本身被传递。 This makes it explicit that the object is passed by reference, and makes understanding and maintaining such code easier. 这使对象明确通过引用传递,并使理解和维护此类代码更加容易。


It is not sane to expect features seen in one programming language to be valid in some other programming language, even if the two are related somehow. 期望在一种编程语言中看到的功能在其他某种编程语言中仍然有效,即使这两者之间存在某种联系也是理智的。

You see, each programming language has their own approach to problem solving, their own paradigm . 您会看到,每种编程语言都有自己的解决问题的方法,自己的范例 Because there is no universal best paradigm possible, problems are best solved using a programming language that has the most applicable/efficient/useful paradigm. 因为没有通用的最佳范例 ,所以使用具有最适用/最有效/最有用范例的编程语言可以最好地解决问题。 For example, you don't write a C++ program to expedite common command-line tasks; 例如,您不必编写C ++程序来加快常见的命令行任务; you use a shell script or other simple scripting language instead. 您可以使用Shell脚本或其他简单的脚本语言来代替。

As a programmer, having the ability to switch from one programming language paradigm to another means you have the ability to look at a problem from different viewpoints. 作为程序员,能够从一种编程语言范例切换到另一种编程语言范例,意味着您能够从不同的角度看待问题。 Looking at current software projects, the most robust, vital, and efficient ones are written by programmers who have that ability. 从当前的软件项目来看,最强大,最重要和最有效的软件是由具有这种能力的程序员编写的。

This is why I stated, above, that it is not sane to expect the features or paradigm of one programming language to be portable to others. 这就是为什么我在上面说过,期望一种编程语言的功能或范例可以移植到其他语言并不明智。 You should not do that, because it is equivalent to having a single tool, and looking at all problems as if your tool at hand is the only possible tool in solving them. 您不应该这样做,因为这等效于拥有一个工具,并且将所有问题视为您手中的工具是解决问题的唯一可能工具。 (If all you have is a hammer, all problems start looking like nails.) Learning, and especially learning to accept, the different paradigms in different programming languages, makes for a better programmer, better problem-solver. (如果您只有锤子,所有问题都会像钉子一样开始。)学习(尤其是学会接受)不同编程语言中的不同范例,将成为一个更好的程序员,一个更好的问题解决者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM