我遇到了一些C++代码.为什么我们必须在块中使用 *this 而不是这个
I have come across some C++ code.Why we have to use *this in block instead of this?
我有以下代码,我想知道为什么它使用*this
而不是this
。
class Quotation
{
protected:
int value;
char* type;
public:
virtual Quotation* clone()=0;
char * getType()
{
return type;
}
int getValue()
{
return value;
}
};
class bikeQuotation : public Quotation
{
public:
bikeQuotation(int number)
{
value=number;
type="BIKE";
}
Quotation * clone()
{
return new bikeQuotation(*this); // <-- Here!
}
};
this
是指向对象的指针。复制构造函数需要对对象的引用。将指针转换为引用的方式是使用取消引用*
运算符。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么使用 "this" 指针调用派生成员函数?
- 编译包含字符串的代码时遇到问题
- 遇到新行时,有没有办法停止istream_iterator
- C++错误:"error: int aaa::bbb is protected within this context"
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 创建结构的数组时遇到分段错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 关于C++中具有多重继承"this"指针的说明
- 在noexcept 规范中是否允许使用"this"?
- 为什么我遇到分段错误?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 为什么我在尝试模板时遇到视觉工作室C++错误
- 我遇到了一些C++代码.为什么我们必须在块中使用 *this 而不是这个