Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reassign `this` pointer inside object member function?

Tags:

c++

pointers

this

I have an interesting question about C++ pointers.

You probably will think that I have to change my design, and avoid doing what I am doing, and you are probably right. But let's assume that I have a good reason to do it my way.

So this is the situation. I have a C++ class TestClass, and I have a pointer A of this type:

TestClass* A = new TestClass();

Among other things TestClass has this function:

void TestClass::Foo(){
    TestClass* B = new TestClass();
    ...
}

This function creates object B of the same type and populates it with some data.

At the end of this function, I want pointer A to point at object B. Anywhere outside this function it would look like A=B; inside this function it could look like this = B
But as you know you cannot reassign "this" pointer.

Possible solutions:

  1. Copy the memory:

    memcpy(this, B, sizeof(TestClass));
    

    This method works correctly. The function copies each bit of object B into object A.
    Problem: if TestClass is a big object(and it is), it creates significant overhead in performance for multiple Foo calls.

  2. Return a B pointer from the function and do something like this

    Temp = A;
    A=A->Foo();
    freeMemory(Temp);
    

    But this code looks stupid, and it makes function Foo very hard to use.

So the question is, how I can do this = B from inside a member function, without copying whole objects?

like image 665
Kirill Avatar asked Jan 12 '10 15:01

Kirill


People also ask

Can you reassign a pointer?

As you know, an address of an object in C++ can be stored either through a reference or through a pointer. Although it might appear that they represent similar concepts, one of the important differences is that you can reassign a pointer to point to a different address, but you cannot do this with a reference.

How can we call member functions through a pointer to an object?

Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);

Which pointer is used in pointer to member function?

The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than . * and ->* , you must use parentheses to call the function pointed to by ptf .

What is the type of this pointer in its member function?

The this pointer is a pointer accessible only within the nonstatic member functions of a class , struct , or union type. It points to the object for which the member function is called. Static member functions don't have a this pointer.


1 Answers

Use an extra level of indirection. Your TestClass can have a pointer that points to a class that contains all of its data.

class TestClass
{
private:
  TestClassData* m_data;

};

void TestClass::Foo()
{
  TestClassData* B = new TestClassData();
  ... 
  delete m_data;
  m_data = B;
} 

Just make sure your operator== returns true if the contents of m_data are equal.

like image 73
Bill Avatar answered Nov 15 '22 09:11

Bill