ccplusplus.com
Learn C, C++ Concepts
Tuesday, May 28, 2013
copy constructor example in c++
/**************************************************************** * File : copy_constructor.cpp * Author : eLearning Division ccplusplus.com * Desc : copy constructor example in c++ * Source : http://www.ccplusplus.com/p/training.html * Created : 12:05 PM 5/28/2013 * Notes : Check the output below ****************************************************************/ #include
#include
//#define USE_COPY_CONSTRUCTOR using std::cout; using std::endl; class CCopyConstructorExlained { private: char *m_cpArr; int m_nLen; public: CCopyConstructorExlained () { } CCopyConstructorExlained (char *cpArr) : m_cpArr (cpArr), m_nLen (0) { m_nLen = strlen (cpArr) + 1; m_cpArr = new char [m_nLen]; strncpy (m_cpArr, cpArr, m_nLen); } #ifdef USE_COPY_CONSTRUCTOR CCopyConstructorExlained (const CCopyConstructorExlained& tmp) { this->m_nLen = tmp.m_nLen; //because m_nLen is not a pointer, we can shallow copy it if (tmp.m_cpArr) // m_cpArr is a pointer, so we need to deep copy it if it is not null { this->m_cpArr = new char [m_nLen]; strncpy (this->m_cpArr, tmp.m_cpArr, this->m_nLen); // Copy the string into our newly allocated memory } else { this->m_cpArr = NULL; } } #endif ~CCopyConstructorExlained () { delete [] m_cpArr; } void m_Print () { cout << m_cpArr << endl; } }; int main () { CCopyConstructorExlained cpy("www.ccplusplus.com"); int nX = 1; if (nX == 1) { CCopyConstructorExlained cpy1 = cpy; // create a new copy from existing object, hence copy constructor } return 0; } /* * Output, when not using user copy constructor * [ccplusplus@rhel6x86 cpp]$ c++ copy_constructor.cpp -o copy_constructor copy_constructor.cpp: In function âint main()â: copy_constructor.cpp:57: warning: deprecated conversion from string constant to âchar*â [ccplusplus@rhel6x86 cpp]$ ./copy_constructor *** glibc detected *** ./copy_constructor: double free or corruption (fasttop): 0x09636008 *** ======= Backtrace: ========= /lib/libc.so.6[0x78e041] /usr/lib/libstdc++.so.6(_ZdlPv+0x22)[0x3ee592] /usr/lib/libstdc++.so.6(_ZdaPv+0x1e)[0x3ee5ee] ./copy_constructor[0x8048746] ./copy_constructor[0x804865e] /lib/libc.so.6(__libc_start_main+0xe6)[0x735cc6] ./copy_constructor[0x8048571] ======= Memory map: ======== 0033f000-00420000 r-xp 00000000 fd:00 1721202 /usr/lib/libstdc++.so.6.0.13 00420000-00424000 r--p 000e0000 fd:00 1721202 /usr/lib/libstdc++.so.6.0.13 00424000-00426000 rw-p 000e4000 fd:00 1721202 /usr/lib/libstdc++.so.6.0.13 00426000-0042c000 rw-p 00000000 00:00 0 006f9000-00717000 r-xp 00000000 fd:00 166640 /lib/ld-2.12.so 00717000-00718000 r--p 0001d000 fd:00 166640 /lib/ld-2.12.so 00718000-00719000 rw-p 0001e000 fd:00 166640 /lib/ld-2.12.so 0071f000-008a9000 r-xp 00000000 fd:00 166641 /lib/libc-2.12.so 008a9000-008ab000 r--p 0018a000 fd:00 166641 /lib/libc-2.12.so 008ab000-008ac000 rw-p 0018c000 fd:00 166641 /lib/libc-2.12.so 008ac000-008af000 rw-p 00000000 00:00 0 008b1000-008d9000 r-xp 00000000 fd:00 166653 /lib/libm-2.12.so 008d9000-008da000 r--p 00027000 fd:00 166653 /lib/libm-2.12.so 008da000-008db000 rw-p 00028000 fd:00 166653 /lib/libm-2.12.so 00be4000-00be5000 r-xp 00000000 00:00 0 [vdso] 00db9000-00dd6000 r-xp 00000000 fd:00 166656 /lib/libgcc_s-4.4.5-20110214.so.1 00dd6000-00dd7000 rw-p 0001d000 fd:00 166656 /lib/libgcc_s-4.4.5-20110214.so.1 08048000-08049000 r-xp 00000000 fd:02 280229 /home/sgupta/Documents/study/cpp/copy_constructor 08049000-0804a000 rw-p 00000000 fd:02 280229 /home/sgupta/Documents/study/cpp/copy_constructor 09636000-09657000 rw-p 00000000 00:00 0 [heap] b7753000-b7756000 rw-p 00000000 00:00 0 b7778000-b7779000 rw-p 00000000 00:00 0 bf8db000-bf8f0000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped) [ccplusplus@rhel6x86 cpp]$ */ /* * Output, when using user copy constructor * [ccplusplus@rhel6x86 cpp]$ c++ copy_constructor.cpp -o copy_constructor -DUSE_COPY_CONSTRUCTOR copy_constructor.cpp: In function âint main()â: copy_constructor.cpp:57: warning: deprecated conversion from string constant to âchar*â [ccplusplus@rhel6x86 cpp]$ ./copy_constructor [ccplusplus@rhel6x86 cpp]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment