python - 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++?
解决方案
但我不知道有任何指向类本身的指针
类不是对象,因此不能有指向类的指针。
在其他成员函数中(即您首先可以访问的唯一this
位置),您可以简单地通过它们的名称来引用静态成员函数:
struct S {
static void foo(){}
void bar() {
foo();
}
};
推荐阅读
- python - 如何在 python 脚本中激活 venv
- python - 3D 弯曲通道的最小横截面——Python 中的解决方案?
- excel - 计算是否或任何其他替代方案以使其更快地工作
- python-2.7 - 如何编译可用的python?
- java - 执行结束时,日志会立即全部转储到控制台,而不是实时打印
- java - 如何从 Java 中的 Kotlin 高阶函数正确显示参数的名称
- reactjs - React Native with Redux 问题与 reducer 用于将项目添加到收藏夹
- python - 使用匹配的关键字对列表中的项目进行排序
- android - Android单元测试问题:没有注册仪器!必须在注册仪器下运行
- javascript - Select2 转换数据