객체의 생성, 소멸, 카피 시점을 쉽게 알 수 있는 클래스 VMyInt

gilgil의 이미지

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: 

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.