0
#include<iostream> 
using namespace std; 

class Test 
{ 
  private: 
  int x; 
  public: 
  Test(int x = 0) { this->x = x; } 
  void change(Test *t)
  { 
    this = t; //line 1 
  } 
  void print() { cout << "x = " << x << endl; } 
}; 

int main() 
{ 
  Test obj(5); 
  Test *ptr = new Test (10); 
  obj.change(ptr); 
  obj.print(); 
  return 0; 
} 

since we know that this pointer hold the reference of calling object. In line 1 i am trying to change the reference of calling object but it shows an error "lvalue required". Can someone explain this??

gsamaras
  • 66,800
  • 33
  • 152
  • 256
chandan
  • 13
  • 3
  • 3
    `this = t` makes no sense, perhaps you meant `*this = *t`? – Some programmer dude Dec 21 '18 at 07:58
  • 1
    And a general tip: Stop using pointers. Except for polymorphism, pointers in C++ are rarely needed. – Some programmer dude Dec 21 '18 at 07:58
  • 2
    `this` is a `Test * const`, so cannot be assigned to. Doing so (if it was allowed) would effectively allow an object to change its own address in memory. You probably mean `*this = *t` or (more explicitly) `this->x = t->x`. – Peter Dec 21 '18 at 08:04
  • @Peter, answers in the answers section please. – Bathsheba Dec 21 '18 at 08:04
  • @Someprogrammerdude Even with polymorphism, references can often be used instead of pointers. Pointers are still useful in various cases where you want to be able to rebind the reference, but even then smart pointers instead of raw pointers are often the right choice. – Daniel H Dec 21 '18 at 08:09
  • Thanks a lot... i understood the logic behind it – chandan Dec 21 '18 at 09:04

3 Answers3

5

You cannot assign a pointer to this pointer, because it's a prvalue.

this pointer is a constant pointer that holds the memory address of the current object. As a result, this is of type const Test* in your case, so it cannot be assigned to. Doing so (if it was allowed) would effectively allow an object to change its own address in memory, as @Peter mentioned.

Note: const Test* is a pointer to a constant object. The object it points to is constant, not the pointer itself.

PS: this->x = t->x; is probably what you meant to say.

gsamaras
  • 66,800
  • 33
  • 152
  • 256
  • 1
    @Bathsheba my mother is a teacher, not an Oxford prof! If you said laconic, I would understand :) Maybe I should rollback? I like the laconic answers. – gsamaras Dec 21 '18 at 08:01
  • 1
    Oh this is getting better and better. Care to add ; informally a `const Test *.`? – Bathsheba Dec 21 '18 at 08:05
  • A `const Test *` can be assigned to. You just cant modify the `Test`instance it points to. So this is not a good answer, I don't see what you got so many upvotes for. – Sid S Dec 21 '18 at 08:45
  • @SidS paraphrased that, sorry. Answer improved now, thanks! – gsamaras Dec 21 '18 at 08:51
  • @gsamaras, Your answer still doesn't make any sense, but the upvotes keep coming in. How do you do it ? – Sid S Dec 21 '18 at 08:53
  • @SidS that's the good think in a democracy. :) If you have a suggestion, in improving my answer, please let me know. – gsamaras Dec 21 '18 at 08:56
  • @gsamaras, Deleting it would be an improvement. What you're saying simply doesn't make any sense. – Sid S Dec 21 '18 at 08:58
1

Here you are assigning a pointer(here t) to "this" pointer for a particular object. "this" pointer is const. pointer that holds the memory address of the current object. You simply can't change the this pointer for an object, since doing this you will practically be changing the location of the object in the memory keeping the name same.

Reference - ‘this’ pointer in C++

-1
#include <iostream>
using namespace std;

class Test 
{ 
  private: 
  int x; 
  public: 
  Test(int x=0) 
  {
     this->x = x;
  } 
  void change(Test *t)
  { 
    t->x; //t is a pointer. so make it point to x
  } 
  void print() { cout << "x = " << x << endl; } 
}; 

 int main() 
{ 
 Test obj(5); 
 Test obj1(10); //create a new object
 Test *ptr = &obj1;//make the pointer point to obj1
 obj.change(ptr); //use change() to point to argument of obj1
 obj.print(); //print the value of obj now
 return 0; 
 } 
  • Rather than just posting a code snippet, add some description on what was wrong/changed compared to the original code and explain it. Like this it is not obvious what even changed without having to compare line by line the snippet to the original code. – huserben Dec 21 '18 at 09:06
  • @Saumya Verma i don't understand what do you want to say. – chandan Dec 21 '18 at 09:08