Question

In Java, when you assign one object to another, the original object isn't copied, it merely clones the reference. So, for example, I'd like it to behave like this Java code:

SomeClass x = new SomeClass();
SomeClass y;
y = x; // x is not copied, y is simply a (Java) reference to x

What I'd like to do is create a C++ class that behaves the same way. The obvious choice is to overload the assignment operator, like so:

SomeClass& operator=(const SomeClass& rhs)
{
    this = &rhs;
    return *this;
}

Unfortunately, assigning a new location to this is not allowed; the above code won't even compile.

Does anyone know of any other way to do this?

Also, before you say anything: yes, I know this is absolutely the wrong way to implement an assignment operator. Please, indulge me anyway.

EDIT 2: To clarify, here is the C++ code that should behave as in Java:

SomeClass x = SomeClass(); // Created on stack
SomeClass y; // In C++, same as above; NOT reference or pointer
y = x; // x is not copied, y becomes a (C++) reference to x;
       // original y is destroyed when stack unwinds

I DO NOT want to use pointers at all.

EDIT: I'm doing this to see if I can change the base address of a C++ object that was passed by reference to a function. If I simply assign another object to the reference variable, it automatically makes a shallow copy, and I don't want that. I want the reference parameter to reference a completely different object.

I'm doing this to see how references are implemented by the compiler. If references are syntactic sugar for dereferenced pointers, then outside the function, the base address of the argument object would NOT change. If they are aliases in the symbol table (as in PHP), then it WILL change. (So any solution using pointers is out, since that's my "control" for the test.)

Hope that makes sense.

Was it helpful?

Solution

This isn't possible. The languages just work fundamentally differently.

SomeClass x in Java is some kind of pointer or reference to a SomeClass object. It's an indirection, so that why you can have multiple ones refering to the same object.

SomeClass x in C++ literally is the object. Thus SomeClass y is literally an entirely different object. There's no indirection. And so there's no way to make one reference the other.

C++ provides pointers (SomeClass* x) and references (SomeClass& x) to take care of the times when indirection is needed. Perhaps those are what you actually want to use, although it depends on why you asked this question in the first place.


Responding to the edit:

No, you can't change the address of an object. An instance of an object will live in exactly one place for the duration of its lifetime, until it is destroyed/deallocated.

OTHER TIPS

I don't think this is okay, because even you assign this to another pointer, then the current object pointed by this will be memory leaked. Java has memory collection, but C++ not.

This is a good question, through. And this is possible.

A& operator=(const A& rhs)
{
    A * cthis = const_cast<A*>(this);
    cthis = const_cast<A*>(&rhs);
    return *cthis;
}

Edit: It is possible to change "this pointer" inside a member function. But changing "this pointer" doesn't accomplish much.

It can be done. but, with pointer indirection.

Lets say you have a class someClass

class someClass
{
   someClass *someClsPtr;

   public:

   someClass(someClass *someClsPtrTemp):someClsPtr(someClsPtrTemp)
   {

   }

   someClass* operator=(someClass *someClsPtrTemp)
   {
       this->someClsPtr = someClsPtrTemp->someClsPtr;
       return this;
   }

   //now over load operator -> and operator *. Thats it.
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top