What is the "minimal structure" (necessary methods) of a complex object (with explicitly specified internal data) that I want to store in an STL container, for example. <vector> ?
For my guesses (an example of a complex Doit object):
#include <vector>
gives
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0804b008 *** Aborted
and in valgrind:
malloc/free: 2 allocs, 12 frees, 50 bytes allocated.
UPDATE:
Minimum methods for such an object: (based on sbi's answer)
class DoIt{ private: char *a; public: DoIt() { a=new char[10]; } ~DoIt() { delete[] a; } DoIt(const DoIt& rhs) { a=new char[10]; std::copy(rhs.a,rhs.a+10,a); } DoIt& operator=(const DoIt& rhs) { DoIt tmp(rhs); swap(tmp); return *this;} void swap(DoIt& rhs) { std::swap(a,rhs.a); } };
Thanks, sbi, https://stackoverflow.com/users/140719/sbi
c ++ copy-constructor rule-of-three stl
osgx
source share