在 c++ 中正确定义"this"关键字?
Proper definition of "this" keyword in c++?
我正在学习OOP C++编程背后的一些理论。我们的教授为我们提供了一些示例问题,以便我们可以在考试前进行复习。你能看看,看看我对这个词的理解是否正确吗?我真的很感激任何建议。
The this keyword:
a) Inside a constructor, it is a reference to currently constructed object. (false)
b) In the method, it is a reference to the object for which it was called (true)
c) Inside the constructor it is a pointer to currently constructed object. (false)
d) In the method, it is a pointer to the object for which it was called. (false)
简单来说(这里不包括虚函数的复杂性(:
this
是一个指针,指向构造的对象,这是传递给任何非静态成员函数的第一个参数。
举个例子,
class X { void foo() {} } ;
X x;
当你做x.foo()
时,foo(( 的第一个不可见参数等于&x
。当你做x.foo()
时,你实际上是在汇编级别做foo(&x)
。
构造函数在技术上(在程序集级别(只是一个函数,就像任何其他成员函数一样,唯一的区别是它在对象构造上被调用。它还像任何其他非静态成员一样采用 this 指针。
所以,你的问题,c(和d(是正确的。
相关文章:
- 在 c++ 中正确定义"this"关键字?
- "this"关键字在C++中的实现限制,因为它与危险但功能示例有关
- 带有 "this" 关键字的 Java Copy 构造函数
- "this"嵌套类中的关键字C++
- C++指针上的关键字运算符"this"
- 在普通 C 中使用 'this' 关键字
- 初始化列表中常量变量的"this"关键字
- 我应该使用 "this" 关键字从成员函数内部访问类成员吗?
- 使用 "this" 关键字时的"Error: expression must have a pointer type"
- 访问修饰符在继承中的不同行为取决于"this"关键字和模板或缺少它们
- 是否可以更改'this'关键字的类型?
- 使用"this"关键字从矢量中删除元素
- 使用 This 关键字和运算符重载
- 在结构构造函数中使用"this"关键字编译错误?-C++
- "this"关键字导致分段错误
- 是否可以在C++中强制使用 "this" 关键字?
- 使用this关键字编译程序
- 在 c++ 中使用 "this" 关键字对性能有影响吗?
- 在C++中使用"this"关键字
- C++中"this"关键字的使用问题