Deep copying and Conversion
Shallow and Deep Copying
Rule of 3
Example of Deep Copy
//@author Erel Segal-Halevi
#include <iostream>
using namespace std;
class IntList {
private:
int* theInts;
uint numInts;
public:
IntList(uint newNumInts):
numInts(newNumInts),
theInts(new int[newNumInts]){ }
// conversion constructor
IntList(const string& other):
numInts(2),
theInts(new int[2]){
cout << "converting string to IntList" << endl;
}
IntList(const IntList& other):
// IntList(IntList other):
IntList(other.numInts){
cout << "copy constructor doing deep copy" << endl;
for (uint i=0; i<numInts; ++i)
theInts[i] = other.theInts[i];
}
IntList& operator=(const IntList& other) {
if (this==&other)
return *this;
cout << "assignment operator doing deep copy..." << endl;
if (other.numInts!=this->numInts) {
delete[] theInts;
theInts = new int[other.numInts]; // init
numInts = other.numInts;
}
for (uint i=0; i<numInts; ++i)
theInts[i] = other.theInts[i];
return *this;
}
~IntList() {
delete[] theInts;
}
int size() const {
return numInts;
}
void operator=(int value) {
cout << "filling with "<<value<<"..."<<endl;
for (uint i=0; i<this->numInts; ++i)
theInts[i] = value;
}
int operator[](uint index) const {
return theInts[index];
}
int& operator[](uint index) {
return theInts[index];
}
void operator()(const IntList& other) {
cout << "hahaha" << endl;
}
};
/**
* If you pass a parameter by value -
* the compiler will call the copy ctor
*/
int sum(const IntList& list) {
int result=0;
for (int i=0; i<list.size(); ++i) {
result += list[i];
}
// list[5] = 88;
return result;
}Conversions of types
Implicit casting
explicit keyword
explicit keywordLast updated