I'm trying to create am immutable type (class) in C++,
I made it so that all methods "aka member functions" don't modify the object and return a new instance instead.
I'm running across a bunch of issues, but they all revolve around the reference types in C++.
One example is when passing parameters of the same class type by reference:
Imm Imm::someOp( const Imm& p_im ) const
{
...
p_im = p_im.someOtherOp(); //error, p_im is const, can't modify it!
...
}
The error is caused by passing the value by reference. If instead, I was passing the reference by value, then the error line above would not be an error!
Consider a Java/C# example
class Imm
{
...
Imm someOp( Imm p_im )
{
....
p_im = p_im.someOtherOp(); //ok, you're not modifying the
//original object, just changing the local reference
....
}
....
}
How can I do something like this in C++? I know I can use pointers but then I run into the whole memory management mess. I don't want to worry about who owns references to objects.
Ideally I'd like to design the class to be like immutable strings in python; you can use them without ever noticing or even knowing that they're immutable, and they just behave as you expect; they just work.
EDIT
Of course I can get around it by passing-by-value or by using a temp variable (which is what I'm doing currently). What I'm asking about is "how to pass references by value in C++"
I'm expecting the answer to revolve around something in the STL, I'm currently looking into smart_ptr family of templates.
UPDATE
Thanks for the responses, I realize there's no escape from pointers. (see my other question, which is really a follow up on this one)