- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我使用 C++ 编程多年,但我仍然对一件事存有疑问。在其他人代码的许多地方,我看到类似:
void Classx::memberfunction()
{
this->doSomething();
}
如果我需要导入/使用该代码,我只需删除 this-> 部分,我从未见过任何损坏或有副作用。
void Classx::memberfunction()
{
doSomething();
}
那么,你知道使用这种结构的任何理由吗?
编辑:请注意,我在这里谈论的是成员函数,而不是变量。我知道当您想区分成员变量和函数参数时可以使用它。
编辑:明显重复: Are there any reasons not to use "this" ("Self", "Me", ...)?
最佳答案
真正发挥作用的唯一地方是派生类中的模板:
template<typename T>
class A {
protected:
T x;
};
template<typename T>
class B : A<T> {
public:
T get() {
return this->x;
}
};
由于 details in the name lookup in C++ compilers ,必须明确说明 x
是类的(继承的)成员,使用 this->x
最容易做到。但这是一个相当深奥的案例,如果您没有模板化的类层次结构,您实际上不需要显式使用 this
来访问类的成员。
关于c++ - 有什么理由使用这个->,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/577243/
您好,我希望我的下一个输出(在本例中就是字母)在上一个输出之后输出 8 个空格。这适用于第一个字符,但之后的 printf 语句不起作用。它在第一个 printf 语句之后立即打印,我试图将其设置为
我想知道制作 std::list<>::splice 背后的基本原理是什么使引用被拼接到新容器中的子序列的迭代器无效。这对我来说有点不合逻辑,尤其是考虑到标准 std::container::swap
谁能告诉我为什么我应该使用 Azure Function 输出绑定(bind)(例如 SendGrid 或 Twilio)而不是仅仅在我的 C# 函数中显式使用适当的 SDK(例如 Sendgrid
我们在当前项目中使用 React 和 TypeScript,我遇到了以下行为。 import React, { Component } from 'react'; 我将上面的行替换为下面的行,因为它似
我是一名优秀的程序员,十分优秀!