C ++相当于Java这个 [英] C++ equivalent to Java this
本文介绍了C ++相当于Java这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,您可以通过执行以下操作来引用当前对象: this.x = x
。你如何在C ++中做到这一点?
In Java you can refer to the current object by doing: this.x = x
. How do you do this in C++?
假设每个代码示例都是 Shape
类的一部分。
Assume that each of these code examples are part of a class called Shape
.
Java:
public void setX(int x)
{
this.x = x;
}
C ++:
public:
void setX(int x)
{
//?
}
推荐答案
同义词: this
只是区别是它是一个指针,所以你需要使用 - >
运算符:
Only difference is it is a pointer, so you need to use the ->
operator:
void setX(int x)
{
this->x = x;
}
这篇关于C ++相当于Java这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文