-2

I am working on C++11 application. I am implementing assignment operator. I want that if I modify one value after the assignment both objects get modified.

Example:

Object o1, o2;
o1.setId("id1");
o2 = o1;
o2.setId("id2");
cout << o1.getId();  ------> this returns id2

How could I achieve that? any ideas

Thanks in advance

YSC
  • 38,212
  • 9
  • 96
  • 149
RuLoViC
  • 825
  • 7
  • 23

3 Answers3

4

To allow multiple instances to share a common object, you might want to use a std::shared_ptr<T> for its internal state:

struct Object
{
    Object() : _data(new std::string()) {}
    void setId(const std::string& value) { *_data = value; }
private:
    std::shared_ptr<std::string> _data;
};

Copying an Object do shallow-copy its internal data as asked:

int main()
{
    Object o1, o2;
    o1.setId("1");
    o2 = o1;
    Object o3(o1);
    o2.setId("2");

    std::cout   << "o1: " << *o1._data << "\n"
                << "o2: " << *o2._data << "\n"
                << "o3: " << *o3._data << "\n"; // Object._data set to public: for this to work
}

This prints:

2
2
2
YSC
  • 38,212
  • 9
  • 96
  • 149
  • You don't even need to define the copy constructor or copy operator here; it would just work. Your defined operators are equivalent to the defaults – Justin Feb 02 '17 at 20:28
  • I know about the rule of five, my point is that you define operators that replicate the default functions the compiler would generate. – Justin Feb 02 '17 at 20:30
  • A `shared_ptr`, just like a regular pointer, does a shallow copy by default as Justin said. No need to implement any of the special member functions. – NathanOliver Feb 02 '17 at 20:32
  • @NathanOliver It's late here at home, fixed; I shouldn't be on the computer. – YSC Feb 02 '17 at 20:34
  • You should remove copy and assignment, otherwise you lose move copy and move assignment. – juanchopanza Feb 02 '17 at 20:42
  • 4
    No, remove them. There is no reason to have them. You don't have to write all that code. – juanchopanza Feb 02 '17 at 20:44
  • @juanchopanza done – YSC Feb 22 '17 at 17:37
1

Use a reference:

Object o1;
o1.setId("id1");
Object &o2 = o1;
o2.setId("id2");
cout << o1.getId(); // id2

A reference is effectively another name to the original object.

Justin
  • 24,288
  • 12
  • 92
  • 142
  • 2
    This is not an answer of the question – Slava Feb 02 '17 at 20:40
  • @Slava How is this not an answer? This solves the problem that the OP described, although not by the method the OP was thinking of. It's an XY problem – Justin Feb 02 '17 at 22:42
  • Code shown is to describe what OP needs and not the actual problem obviously. Your "solution" would not work for a container for example. You just provided formal answer for the code, not actual problem. – Slava Feb 02 '17 at 23:24
-2

As far as I know, "shallow copy" is not

...if I modify one value after the assignment both objects get modified.

Shallow copy of any value(pointer) is simply a copy of a value(pointer). Shallow copy of some class object is just a memberwise copy.

Default assignment operator does the trick.

In case if you came from a C#-like land:

When you write

Object o1;
Object o2;

you have already created two indepenent objects in memory (on stack). And when you write

o2 = o1; 

by default you do a shallow copy: just copy all data stored in a chunk of memory corresponding to o1 to the memory corresponding to o2. As simple as that. And essentially C# does the same, the only difference is that o1 and o2 are references there. So you just copy a value of one reference (not the value reference is referring to!) to another reference.

So the point is that it is all not about

...implementing assignment operator.

but copying references or object values itself. In c++ writing o1 you get the object value, not reference to the object. Moreover, in c++ you can not copy references. But what you can is copying pointer.

Object* o1 = new Object;
Object* o2;
o2 = o1; // now pointing to the same object

But keep in mind, c++ does not have a GC, so each time you allocate memory on heap you must dealocate it later by yourself (unless you use smart pointers but that is another topic).

WindyFields
  • 2,697
  • 1
  • 18
  • 21
  • This does not answer the question, it only nitpick over what a shallow-copy is. If OP uses the expression _shallow copy_ in the title, they don't use it again in the question: they explicitly ask how to achieve a particular behaviour ; your answer does not tell anything regarding the desired behaviour. – YSC Feb 06 '17 at 14:47
  • What is said here is true and could be useful, but it just doesn't answer the question. – YSC Feb 06 '17 at 14:51
  • So I should just have copied your obvious answer instead of trying to approach a question in a different way... I see. Just thought if people asks "how to make assignment operator to make a shallow copy" they just don`t understand some fundamental things. So it is an answer. – WindyFields Feb 06 '17 at 22:19