ccplusplus.com
Learn C, C++ Concepts
Tuesday, May 28, 2013
overloaded assignment operator in c++ with example
/**************************************************************** * File : assignment_operator.cpp * Author : eLearning Division ccplusplus.com * Desc : assignmemnt operator explained * Source : http://www.ccplusplus.com/p/training.html * Created : 12:38 PM 5/28/2013 * Notes : Check the output below ****************************************************************/ #include
#include
using std::cout; using std::endl; class COverloadedAssignmentOperator { private: char *m_cpArr; int m_nLen; public: COverloadedAssignmentOperator () { } COverloadedAssignmentOperator (char *cpArr = NULL) : 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 COverloadedAssignmentOperator (const COverloadedAssignmentOperator& tmp); #endif //USE_COPY_CONSTRUCTOR #ifdef USE_OVERLOAD_ASSIGNMENT COverloadedAssignmentOperator& operator = (const COverloadedAssignmentOperator& tmp); #endif //USE_OVERLOAD_ASSIGNMENT ~COverloadedAssignmentOperator () { delete [] m_cpArr; } void m_Print () { cout << m_cpArr << endl; } }; #ifdef USE_COPY_CONSTRUCTOR COverloadedAssignmentOperator :: COverloadedAssignmentOperator (const COverloadedAssignmentOperator& 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 //USE_COPY_CONSTRUCTOR #ifdef USE_OVERLOAD_ASSIGNMENT COverloadedAssignmentOperator& COverloadedAssignmentOperator :: operator = (const COverloadedAssignmentOperator& tmp) { // check for self assignment if (this == &tmp) { return *this; } // delete any values that string holds currently delete [] m_cpArr; 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; } return *this; } #endif //USE_OVERLOAD_ASSIGNMENT int main () { COverloadedAssignmentOperator cpy("www.ccplusplus.com"); int nX = 1; if (nX == 1) { COverloadedAssignmentOperator cpy1 ("hello"); cpy1 = cpy; } return 0; } /* * Output, when not using user copy constructor * [ccplusplus@rhel6x86 cpp]$ c++ assignment_operator.cpp -o assignment_operator -DUSE_COPY_CONSTRUCTOR assignment_operator.cpp: In function âint main()â: assignment_operator.cpp:94: warning: deprecated conversion from string constant to âchar*â assignment_operator.cpp:98: warning: deprecated conversion from string constant to âchar*â [ccplusplus@rhel6x86 cpp]$ ./assignment_operator *** glibc detected *** ./assignment_operator: double free or corruption (fasttop): 0x08113008 *** ======= Backtrace: ========= /lib/libc.so.6[0x78e041] /usr/lib/libstdc++.so.6(_ZdlPv+0x22)[0x3ee592] /usr/lib/libstdc++.so.6(_ZdaPv+0x1e)[0x3ee5ee] ./assignment_operator[0x8048838] ./assignment_operator[0x8048732] /lib/libc.so.6(__libc_start_main+0xe6)[0x735cc6] ./assignment_operator[0x80485d1] ======= Memory map: ======== 0012f000-00130000 r-xp 00000000 00:00 0 [vdso] 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 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 280526 /home/ccplusplus/Documents/study/cpp/assignment_operator 08049000-0804a000 rw-p 00000000 fd:02 280526 /home/ccplusplus/Documents/study/cpp/assignment_operator 08113000-08134000 rw-p 00000000 00:00 0 [heap] b77b7000-b77ba000 rw-p 00000000 00:00 0 b77dc000-b77dd000 rw-p 00000000 00:00 0 bfef9000-bff0e000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped) [ccplusplus@rhel6x86 cpp]$ */ /* * Output, when using user copy constructor * [ccplusplus@rhel6x86 cpp]$ c++ assignment_operator.cpp -o assignment_operator -DUSE_OVERLOAD_ASSIGNMENT -DUSE_COPY_CONSTRUCTOR assignment_operator.cpp: In function âint main()â: assignment_operator.cpp:94: warning: deprecated conversion from string constant to âchar*â assignment_operator.cpp:98: warning: deprecated conversion from string constant to âchar*â [ccplusplus@rhel6x86 cpp]$ ./assignment_operator [ccplusplus@rhel6x86 cpp]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment