1

Hello World, I have been wondering why in c++ we use the this->ObjectName instead of this.ObjectName when referring to an object in c++. This is a simple syntax using the this->ObjectName :

     int x = 34;
     void take(int x) {
      this->x = x;
     }

But in other programming languages like java, we use :

     int x = 34;
     void take(int x) {
      this.x = x;
     }

So what am asking is why does the this statement in c++ differ from the this statement in java

Favor
  • 810
  • 3
  • 11
  • 18
  • 4
    Because C++ is not Java and `this` is a pointer. – Captain Obvlious May 15 '14 at 16:58
  • It's not unreasonable to imagine that different languages have different syntax. In c++, `->` dereferences a pointer + accesses a property, where `.` just accesses a property. – Sam Dufel May 15 '14 at 16:58
  • From Stroustrup, *The C++ Programming Language, Fourth Edition*, page 465, "... the keyword *this* is a pointer to the object for which the function was invoked." – CPlusPlus OOA and D May 15 '14 at 17:02
  • To the close voters, this is definitely not "primarily opinion based". – juanchopanza May 15 '14 at 17:07
  • @juan I voted to close as opinion based because of the *why doesn't this work like it does in Java?* question. But maybe it's better to mark it as a dupe of [this](http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&sqi=2&ved=0CCkQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F645994%2Fwhy-this-is-a-pointer-and-not-a-reference&ei=lfR0U9ibCsj_oQSj8YGwCQ&usg=AFQjCNF_DHE_cHuvE86Ewd9ylCGme1c1rg&sig2=0kGt8NaObM2fHi0MnpS2MA&bvm=bv.66699033,d.cGU)? – Praetorian May 15 '14 at 17:10
  • Thanks guy, I am anyway just a beginner in c++, i should have put that in mupy question but am a pro in java and thanks anyway I just learnt about the c++ arrow member selection operator than I wanted to know why it was so different from java but thanks anyway. – Favor May 15 '14 at 17:11
  • @user3526881: How can you be a "pro" in Java without having written native JNI code in C or in C++, where you usually deal with pointers all the time? – Christian Hackl May 15 '14 at 17:14
  • @Praetorian I think the duplicate is a better close reason. – juanchopanza May 15 '14 at 17:37

3 Answers3

3

This only applies to pointers. Expressions of class/struct type (references inclusive) use the more familiar dot notation:

class Whatever {
 public:
  int x;
  void take(int n);
};

class T {
  public:
   int x;
};

void Whatever::take(int n) {
  T t;
  T *pt = &t; // pointer
  T &tr = t;  // reference is basically just an alias

  pt->x   = n;
  (*pt).x = n; // can dereference a pointer, then use like a reference
  tr.x    = n;

  // Same for this.
  this->x   = n;
  (*this).x = n;
}

What may seem odd to you is that this is a pointer in C++ as opposed to a reference.

See also: Why 'this' is a pointer and not a reference? (Stroustrup introduces this before he created references in the language.)

Community
  • 1
  • 1
Jeff
  • 3,475
  • 4
  • 26
  • 35
  • 1
    It's not so much "references" as it is "values of class type". Being a reference is a property of a variable, not of an expression. You can say `foo().x += 1`, for example... – Kerrek SB May 15 '14 at 17:19
  • Good point, clarification added. Thanks! – Jeff May 15 '14 at 17:22
2

In Java/C#, almost everything is of reference type (except the primitive ones, struct in c# etc.). But in C++, both value and reference semantics are used. Use

Myclass{ 
 public: void foo(){} 
};

Myclass ob; //as value
ob.foo();

and

Myclass * ob; //as pointer
ob->foo(); 

Note: except pointer, there is another way for reference usage in C++, which is the reference itself, but in c++, reference is used as an alias for an object, so it is like the value type (like Java/C#).

 Myclass ob; //as value
 Myclass& ref=ob; // reference to ob
 ref.foo();
Rakib
  • 7,435
  • 7
  • 29
  • 45
  • 1
    `struct`s and primitive types are [value types](http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx) in C#. – Praetorian May 15 '14 at 17:07
1

In C++ you have pointers. With pointers -> is used. With references/objects . is used.

Drakosha
  • 11,925
  • 4
  • 39
  • 52
  • Pointers are objects, too... – Kerrek SB May 15 '14 at 17:03
  • That's just how C++ works. Lots of things are objects. – Kerrek SB May 15 '14 at 17:17
  • @KerrekSB: please elaborate, what properties of an object do they have? IMHO they are more like primitives (int) – Drakosha May 15 '14 at 20:43
  • 1
    `int`s are objects, too! Objects are things that are stored in memory (see 1.8/1). There's generally not so much difference between built-in and user-defined types, and that's by design. Same treatment for everyone. – Kerrek SB May 15 '14 at 22:55