Get a copy of "this" (current instance) in C ++

I want to have a copy of the current executable instance.

When I change the value in the copy, the original object is also affected. A copy acts as an instance.

How to avoid this? I need to create an independent copy of the caller.

Set operator+(Set s){ Set temp = *this; for(int i=0; s.elements[i] != '\0'; i++){ temp(s.elements[i]); } temp.elements[0] = 'X'; // <- this affects calling object also :( return temp; } 
+4
source share
6 answers

The problem is that Set temp = *this; makes a shallow copy, not a deep copy. You will have to change the copy constructor and assignment operators for the Set class so that they make copies of all objects containing the / member.

eg:

 class Set { public: Set() { elements = new SomeOtherObject[12]; // Could make elements a std::vector<SomeOtherObject>, instead } Set(const Set& other) { AssignFrom(other); } Set& operator=(const Set& other) { AssignFrom(other); return *this; } private: void AssignFrom(const Set& other) { // Make copies of entire array here, as deep as you need to. // You could simply do a top-level deep copy, if you control all the // other objects, and make them do top-level deep copies, as well } SomeOtherObject* elements; }; 
+8
source

Not that your function already makes two copies , as it takes its argument and returns its result as a copy:

 Set operator+(Set s); 

This way you would not have to copy s because it is already copied. I believe this is involuntary, so you can read about how to pass objects to functions and how to return objects from a function in C ++.

The issue you are reporting, however, suggests that your constructor is not working properly . Have you implemented a copy constructor or are you using a compiler?

+5
source

It probably depends on how Set implemented. If the assignment operator and copy constructor were not overloaded to make a deep copy (including elements ), then it will not work properly.

+3
source

Have you implemented a copy constructor for your class? The copy constructor, by default, copies any pointer to your class, but not the content you are pointing to. You need to create a copy constructor or overload the '=' operator.

+2
source

I would completely avoid the char pointer and use std :: string. This way, you don’t even need a copy constructor and an assistant operator, because a compiler generated once will work just fine. (because the "elements" of the "Set" class are copyable and have an assignment operator) Here is my solution:

 #include <iostream> #include <string> class Set{ std::string elements; public: Set() { elements = ""; } explicit Set(char* _elements) { if (_elements) elements = _elements; } Set operator+(const Set& s){ Set temp(*this); temp.elements += s.elements; return temp; } }; 

Btw. I added a constructor from char *, so that the "elements" can be somehow initialized from the outside. Not sure if this is what you wanted.

+1
source

Ok I went through three rules and made the following changes ... Can you indicate what is wrong with this?

 #include<iostream> #include<cstring> using namespace std; class Set{ char *elements; public: Set() { elements = new char('\0'); index = -1; } Set(const Set& cpy){ *this = cpy; } Set operator+(Set s){ Set temp = *this; // IMPORTANT! copy constructor of Set is called, "this" is passed as argument // * = current OBJECT, else returns ADDRESS of current object for(int i=0; s.elements[i] != '\0'; i++){ temp(s.elements[i]); } return temp; } Set& operator=(Set s){ delete [] elements; elements = new char[strlen(s.elements) + 1]; strcpy(elements, s.elements); //overrides element of "this" return *this; } }; 
0
source

All Articles