首页 > 解决方案 > Shouldn't there be a name-agnostic class pointer similar to 'this' pointer for member functions to refer to in C++?

问题描述

In Python, there's a self variable that refers to the instance itself, and a cls object (for class methods) that refer to the class. In the same manner, there's a 'this' pointer pointing to the calling object; but I'm not aware of any pointer to the class itself, I'm familiar with using something like this...

ClassName::memberFunction(this)

But it's not what I want.

I guess it's pretty obvious that there's no such thing, given how constructors still make use of their class's name, but why not make it a feature in C++?

标签: pythonc++classthis

解决方案


但我不知道有任何指向类本身的指针

类不是对象,因此不能有指向类的指针。

在其他成员函数中(即您首先可以访问的唯一this位置),您可以简单地通过它们的名称来引用静态成员函数:

struct S {
    static void foo(){}
    void bar() {
        foo();
    }
};

推荐阅读