http://www.codeproject.com/KB/cpp/cppproperties.aspx
에 있는걸 참고해서 문제가 있었던 object1.prop = object2.prop의 문제를 수정하였으나
그 이외에도 문제가 많다.


#include <stdio.h>
#include <atlbase.h>
#include <iostream>

/////////////////////////////////////////////////////////////////////////////
//
// Interface of the CProperty<TContainer,TValue> class
//    Ref=http://www.codeproject.com/KB/cpp/cppproperties.aspx
//        
/////////////////////////////////////////////////////////////////////////////
template<typename TContainer, typename TValue>
class CProperty
{
public:
    typedef TValue (TContainer::*SETPROC)(TValue value);
    typedef TValue (TContainer::*GETPROC)(void) const;

private:
    TValue      m_Value;
    SETPROC     m_pSetProc;
    GETPROC     m_pGetProc;
    TContainer* m_pContainer;

public:
    CProperty() :
        m_pSetProc  (NULL),
        m_pGetProc  (NULL),
        m_pContainer(NULL)
    {
    }

    CProperty(const CProperty& o)
    {
        m_Value = o.Get();
    }

public:
    void Bind (TContainer* container, GETPROC getproc, SETPROC setproc)
    {
        m_pContainer = container;
        m_pGetProc   = getproc;
        m_pSetProc   = setproc;
    }

    CProperty<TContainer,TValue> operator = (const CProperty& rhs)
    {
        Set(rhs.Get());
    
        return *this;
    }

    TValue Get (void) const
    {
        ATLASSERT(m_pContainer != NULL);
        ATLASSERT(m_pGetProc   != NULL);

        return (m_pContainer->*m_pGetProc)();
    }

    TValue Set (const TValue& value)
    {
        ATLASSERT(m_pContainer != NULL);
        ATLASSERT(m_pSetProc   != NULL);

        return (m_pContainer->*m_pSetProc)(value);    
    }

    operator TValue (void) const
    {
        return Get ();
    }

    TValue operator = (const TValue& value)
    {
        return Set (value);
    }
};

/////////////////////////////////////////////////////////////////////////////
//
// Interface of the CProperties class
//
/////////////////////////////////////////////////////////////////////////////
class CProperties
{
private:
    int m_Count;

public:
    CProperty<CProperties,int> Count;

public:
    CProperties () : m_Count(0)
    {
        Count.Bind (this, &CProperties::get_Count, &CProperties::set_Count);
    }

public:
    int get_Count (void) const
    {
        printf ("\t\t%08x::get_Count()=%d\r\n", this, m_Count);

        return m_Count;
    }

    int set_Count (int n)
    {
        m_Count = n;

        printf ("\t\t%08x::set_Count(%d)\r\n", this, m_Count);

        return m_Count;
    }
};

/////////////////////////////////////////////////////////////////////////////
//
// Test
//
/////////////////////////////////////////////////////////////////////////////
static int Test (void)
{
    CProperties prop;
    
    prop.Count = 100;

    return prop.Count;
}

/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
    CProperties Properties1, Properties2;

    printf ("\t\tProperties1=%08x\r\n", &Properties1);
    printf ("\t\tProperties2=%08x\r\n", &Properties2);

    printf ("-------------------------------------------------------------\r\n");

    printf ("Test()=%d\r\n", Test());

    printf ("-------------------------------------------------------------\r\n");

    Properties1.Count = 200;

    Properties2.Count = Properties1.Count;

    Properties1.Count = 100;

    std::cout << "Properties1.Count=" <<Properties1.Count << std::endl;
    std::cout << "Properties2.Count=" <<Properties2.Count << std::endl;

    printf ("-------------------------------------------------------------\r\n");

    int n=Properties2.Count;

    printf ("printf() n     =%d\r\n",n);
    printf ("printf() cast  =%d\r\n",(int)Properties2.Count);
    printf ("printf() nocast=%d\r\n",Properties2.Count); // 방법없음

    return 0;
}

Posted by 셈말짓기 :