S
Shailesh Humbad
In the code below, I don't understand how push_front can make a deep
copy of the locally declared dataElement object. I thought the
default copy constructor only copies the members of the class, or is
it just that the local dataElement object persists in the linked list
even after addTo returns. Can someone explain why, or point to an
appropriate section of the C++ faq/reference?
-----------COMPLETE CODE-----------
#include <iostream>
#include <list>
using namespace std;
class dataElement {
public:
char * szM;
};
void addTo(list<class dataElement> &L) {
class dataElement myD;
myD.szM = (char*)malloc(sizeof(char)*3);
myD.szM[0] = 'a';
myD.szM[1] = 'b';
myD.szM[2] = '\0';
L.push_front(myD);
}
int main(int argc, char* argv[])
{
class dataElement deTemp;
list<class dataElement> L;
addTo(L);
deTemp = L.front();
cout << "output: " << deTemp.szM << "\n";
// Prints "output: 100 ab"
return 0;
}
copy of the locally declared dataElement object. I thought the
default copy constructor only copies the members of the class, or is
it just that the local dataElement object persists in the linked list
even after addTo returns. Can someone explain why, or point to an
appropriate section of the C++ faq/reference?
-----------COMPLETE CODE-----------
#include <iostream>
#include <list>
using namespace std;
class dataElement {
public:
char * szM;
};
void addTo(list<class dataElement> &L) {
class dataElement myD;
myD.szM = (char*)malloc(sizeof(char)*3);
myD.szM[0] = 'a';
myD.szM[1] = 'b';
myD.szM[2] = '\0';
L.push_front(myD);
}
int main(int argc, char* argv[])
{
class dataElement deTemp;
list<class dataElement> L;
addTo(L);
deTemp = L.front();
cout << "output: " << deTemp.szM << "\n";
// Prints "output: 100 ab"
return 0;
}