1

I tried copying a pointer to another by using a method inside the class and the this pointer as follows. I am giving the entire test code so that it is clear what is going on.

class test  {
private:
    int x;
public:
    void setx(int x);
    int getx(void);
    void copy(test *temp);
};

void test::setx(int x)  {
    this->x = x;
}

int test::getx(void)    {
    return this->x;
}

void test::copy(test *temp) {
    this = temp;
}

And I access this method from the main as follows:

int main()  {
    test a;
    a.setx(4);
    cout << a.getx()<<endl;
    test *b = new test;
    b->setx(4);
    cout << b->getx()<<endl;
    test *c;
    c=b;
    cout << c->getx()<<endl;
    test *d;
    d->copy(b);
    cout << d->getx()<<endl;
}

However it gives the following error

In member function ‘void test::copy(test*)’:
error: lvalue required as left operand of assignment

All the other method involving the this pointer works fine except for the copying part. Am i doing some elementary mistake in using the this pointer?

3 Answers 3

7

You cannot overwrite this. The this pointer is a constant, so you're not allowed to change it. And what would that mean anyway? You can't change the object that you're in. You can change the values within that object, but not the object itself.

You need to copy other objects by value (by what is stored in the object), not by pointer.

Also, you shouldn't have a function called copy; that's what copy constructors and copy assignment operators are for.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok. But then why is c=b fine and working? Even there I am copying a pointer variable into another right?
this is not a pointer variable. Because it cannot vary. It is a pointer constant, as in unchanging. c and b are variables; this is constant.
Thanks. And sorry I don't have enough reputation to vote your answer up. I have anyways accepted your answer.
Technically, this is not a constant, but a scalar prvalue. But it does not hurt too much if think of it as a constant.
2

You cannot modify the this pointer. You can however modify *this:

void test::copy(test *temp)
{
    *this = *temp;
}

Also, you should rename the data member or the parameter, so you don't need this->:

class test
{
int m_x;
public:
void setx(int x)
{
    m_x = x;
}

1 Comment

Sorry I don't have enough reputation to vote your answer up. It was helpful.
1

what is test::copy supposed to do? Clearly you cant assign a different address to your current object. So it is invalid.

if this is supposed to initialize the current object with the values of some other object then it should look like this:

void test::copy(test *temp) {
    this->x = temp->getX();
}

1 Comment

Sorry I don't have enough reputation to vote your answer up. It was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.