Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent to Java this

Tags:

java

c++

this

In Java you can refer to the current object by doing: this.x = x. How do you do this in C++?

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) { //? } 
like image 838
nobody Avatar asked Aug 01 '11 23:08

nobody


People also ask

What is the equivalent to java this in C++?

A later lecture will introduce classes in C++. The C++ equivalent of the Java import statement is the #include statement.

What is the equivalent to this in C++?

The C++ equivalent is this ; that is, the keyword is the same. Do not forget the -> thats probably what the OP is missing.

What is the equivalent of object in C?

There is nothing equivalent to classes . Its a totally different paradigm. You can use structures in C. Have to code accordingly to make structures do the job.

What is equivalent to pointer in Java?

Java does not have pointers. It has references almost exactly as C++ has.


1 Answers

Same word: this

Only difference is it is a pointer, so you need to use the -> operator:

void setX(int x) {     this->x = x; } 
like image 143
Benjamin Lindley Avatar answered Sep 18 '22 09:09

Benjamin Lindley