Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15530564
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: C/C++

2008-12-10 16:21:48

#include<iostream.h>
using namespace std;

#define COMPARE(_op_) \
inline bool operator _op_ (const sp<T>& o) const { \
    return m_ptr _op_ o.m_ptr; \
} \
inline bool operator _op_ (const T* o) const { \
    return m_ptr _op_ o; \
}
// typename目的是告诉c++编译器,typename后面的字符串为一个类型名称
template <typename T>
class sp
{
public:
    inline sp() : m_ptr(0) { }
    sp(T* other);
    sp(const sp<T>& other);
    // Assignment

    //sp& operator = (T* other);
    inline sp& operator = (T* other) {
        m_ptr = other;
    }
    // sp& operator = (const sp& other);
    inline sp& operator = (const sp<T>& other) {
        m_ptr = other.m_ptr;
    }
    template<typename U> sp& operator = (const sp<U>& other);
    //template sp& operator = (U* other);
    template<typename U> sp& operator = (U* other) {
        m_ptr = (T*)0x88;//只是为了测试[luther.gliethttp]
    }
    // Accessors
    inline T& operator* () const { return *m_ptr; }//返回给引用
    inline T* operator-> () const { return m_ptr; }
    inline T* get() const { return m_ptr; }
    // Operators
    COMPARE(==)
    COMPARE(!=)
    COMPARE(>)
    COMPARE(<)
    COMPARE(<=)
    COMPARE(>=)
private:
    T* m_ptr;
};

template<typename T>
sp<T>::sp(T* other)
    : m_ptr(other)
{
    //if (other) other->incStrong(this);
}

template<typename T>
sp<T>::sp(const sp<T>& other)
    : m_ptr(other.m_ptr)
{
    //if (m_ptr) m_ptr->incStrong(this);
}
/*
//定义于include/utils/RefBase.h文件中[luther.gliethttp].
template
sp& sp::operator = (const sp& other) {
    if (other.m_ptr) other.m_ptr->incStrong(this);
    if (m_ptr) m_ptr->decStrong(this);
    m_ptr = other.m_ptr;
    return *this;
}

template
sp& sp::operator = (T* other)
{
    if (other) other->incStrong(this);
    if (m_ptr) m_ptr->decStrong(this);
    m_ptr = other;
    return *this;
}

template template
sp& sp::operator = (const sp& other)
{
    if (other.m_ptr) other.m_ptr->incStrong(this);
    if (m_ptr) m_ptr->decStrong(this);
    m_ptr = other.m_ptr;
    return *this;
}

template template
sp& sp::operator = (U* other)
{
    if (other) other->incStrong(this);
    if (m_ptr) m_ptr->decStrong(this);
    m_ptr = other;
    return *this;
}
*/


class luther
{
public:
    luther();
    int value;
    int lget(void);
};

struct test_struct {
    int svalue;
};

int luther::lget(void)
{
    return 10;
}

luther::luther()
{
    printf("luther gliethttp created\n");
}

void test_func(luther& v)
{
    printf("%s : &v=%X,%X\n", __func__, &v, v.lget());
}

int main()
{
    sp<luther> a,c;
    luther b;
    struct test_struct ttt;

    printf("a=%s,m_ptr=%X\n", (a == NULL) ? "NULL":"YES", a.get());
    test_func(*a);
    // 对于NULL对象,仍然可以调用对象的方法,但是不能调用对象属性,因为没有给对象属性分配存储空间[luther.gliethttp]
    // 以下就是调用m_ptr为NULL的luther对象方法lget();
    cout << a->lget() << "\n";//调用luther::lget()方法.
    a = &b;
    printf("m_ptr=%X,b=%X\n", a.get(), &b);
    printf("value=%X\n", a->value);
    test_func(*a);
    test_func(b);
    a = c;
    printf("m_ptr=%X\n", a.get());
    test_func(*a);
    a = &ttt;
    printf("m_ptr=%X\n", a.get());
    test_func(*a);

    a = new luther; //从堆上分配空间,然后同时会执行inline sp& operator = (T* other) { m_ptr = other; }赋值操作,将对象地址复制给a.m_ptr[luther.gliethttp].

    printf("m_ptr=%X\n", a.get());
    test_func(*a);
    cout << "ok\n";
    return 0;
}

如下为编译执行后的log:
luther@gliethttp:~/tmp$ g++ luther_gliethttp.cpp
In file included from /usr/include/c++/4.2/backward/iostream.h:31,
                 from luther_gliethttp.cpp:1:
/usr/include/c++/4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated.
luther@gliethttp:~/tmp$ ./a.out
luther gliethttp created
a=NULL,m_ptr=0
test_func : &v=0,A
10
m_ptr=BFD66914,b=BFD66914
value=8049104
test_func : &v=BFD66914,A
test_func : &v=BFD66914,A
m_ptr=0
test_func : &v=0,A
m_ptr=88
test_func : &v=88,A
luther gliethttp created
m_ptr=804A008
test_func : &v=804A008,A
ok
阅读(4770) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~