객체의 생성, 소멸, 카피 시점을 쉽게 알 수 있는 클래스 VMyInt
글쓴이: gilgil / 작성시간: 화, 2012/04/17 - 7:08오후
C++로 프로그래밍을 하다 보면 STL을 사용하거나, Singleton template를 사용하거나, 혹은 객체를 global 혹은 static으로 선언해서 사용하는 경우 해당 객체의 constructor, destructor, copy constructor, assign operator 등이 언제 불리는 지를 알 필요가 있는 경우가 있습니다. 요런 모든 경우를 실제로 확인해 볼 수 있는 VMyInt라는 클래스를 만들어 봤습니다.
출처는 제 홈페이지 : http://www.gilgil.net/15449
// ---------------------------------------------------------------------------- // // VDream Component Suite version 8.0 // // <a href="http://www.gilgil.net" rel="nofollow">www.gilgil.net</a> // // Copyright (c) Gilbert Lee All rights reserved // // ---------------------------------------------------------------------------- #ifndef __V_MY_INT_H__ #define __V_MY_INT_H__ #include <stdio.h> // for vsprintf #include <stdarg.h> // for va_list // ---------------------------------------------------------------------------- // VMyInt // ---------------------------------------------------------------------------- class VMyInt { protected: int m_i; public: VMyInt() { debug("%p VMyInt::VMyInt()\n", this); m_i = 0; } VMyInt(const VMyInt& rhs) { debug("%p VMyInt::VMyInt(VMyInt& %d) %p\n", this, rhs.m_i, &rhs); m_i = rhs.m_i; } VMyInt(const int i) { debug("%p VMyInt::VMyInt(int %d)\n", this, i); m_i = i; } virtual ~VMyInt() { debug("%p VMyInt::~VMyInt() %d\n", this, m_i); } VMyInt& operator = (const VMyInt& rhs) { debug("%p VMyInt(%d)::= (VMyInt& %d) %p\n", this, m_i, rhs.m_i, &rhs); m_i = rhs.m_i; return *this; } VMyInt& operator = (const int i) { debug("%p VMyInt(%d)::= (int %d)\n", this, m_i, i); m_i = i; return *this; } VMyInt& operator ++() { debug("%p VMyInt(%d)::++() > %d\n", this, m_i, m_i + 1); m_i++; return *this; } operator const int() { debug("%p VMyInt::int(%d)\n", this, m_i); return m_i; } int const toInt() { debug("%p VMyInt::toInt(%d)\n", this, m_i); return m_i; } bool operator == (const VMyInt& rhs) const { debug("%p VMyInt(%d)::==(VMyInt& %d) %p\n", this, m_i, rhs.m_i, &rhs); return m_i == rhs.m_i; } bool operator < (const VMyInt& rhs) const { debug("%p VMyInt(%d)::< (VMyInt& %d) %p\n", this, m_i, rhs.m_i, &rhs); return m_i < rhs.m_i; } bool operator > (const VMyInt& rhs) const { debug("%p VMyInt(%d)::> (VMyInt& %d) %p\n", this, m_i, rhs.m_i, &rhs); return m_i > rhs.m_i; } bool operator <= (const VMyInt& rhs) const { debug("%p VMyInt(%d)::<=(VMyInt& %d) %p\n", this, m_i, rhs.m_i, &rhs); return m_i <= rhs.m_i; } bool operator >= (const VMyInt& rhs) const { debug("%p VMyInt(%d)::>=(VMyInt& %d) %p\n", this, m_i, rhs.m_i, &rhs); return m_i >= rhs.m_i; } bool operator != (const VMyInt& rhs) const { debug("%p VMyInt(%d)::!=(VMyInt& %d) %p\n", this, m_i, rhs.m_i, &rhs); return m_i != rhs.m_i; } bool operator == (const int i) const { debug("%p VMyInt(%d)::==(int %d)\n", this, m_i, i); return m_i == i; } bool operator < (const int i) const { debug("%p VMyInt(%d)::< (int %d)\n", this, m_i, i); return m_i < i; } bool operator > (const int i) const { debug("%p VMyInt(%d)::> (int %d)\n", this, m_i, i); return m_i > i; } bool operator <= (const int i) const { debug("%p VMyInt(%d)::<=(int %d)\n", this, m_i, i); return m_i <= i; } bool operator >= (const int i) const { debug("%p VMyInt(%d)::>=(int %d)\n", this, m_i, i); return m_i >= i; } bool operator != (const int i) const { debug("%p VMyInt(%d)::!=(int %d)\n", this, m_i, i); return m_i != i; } public: void debug(const char* fmt, ...) const { static const int BUF_SIZE = 4096; char buf[BUF_SIZE]; va_list args; va_start(args, fmt); #ifdef __STDC_WANT_SECURE_LIB__ int len = vsprintf_s(buf, BUF_SIZE, fmt, args); #else int len = vsprintf(buf, fmt, args); #endif // __STDC_WANT_SECURE_LIB__ if (len != -1) printf("%s", buf); va_end(args); } }; #endif // __V_MY_INT_H__
Forums:
댓글 달기