设为首页 加入收藏

TOP

调试秘笈-内存泄露原因(三)
2013-04-24 12:13:00 来源: 作者: 【 】 浏览:987
Tags:调试 秘笈 内存 泄露 原因

 

    {

    SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator -> on NULL pointer.");

    return *ptr_;

    }

    T* operator -> () const

    {

    SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator -> on NULL pointer.");

    return ptr_;

    }

    ~Ptr()

    {

    }

    private:

    T* ptr_;

    };

    } // namespace scpp

    #endif // __SCCP_REFCOUNTPTR_H__

    测试代码(vs2012+win7环境):

    [cpp]

    #include "stdafx.h"

    #include "scpp_assert.h"

    #include "iostream"

    #include "scpp_vector.h"

    #include "scpp_array.h"

    #include "scpp_matrix.h"

    #include "algorithm"

    #include "scpp_types.h"

    #include "scpp_refcountptr.h"

    #include "scpp_scopedptr.h"

    #include "scpp_ptr.h"

    #define STUDENTAGE 10

    class Student

    {

    public:

    Student(int age) : age_(age)

    {

    }

    void ShowAge()

    {

    std::cout 《 "my age is : " 《 age_ 《 std::endl;

    }

    int GetAge() const

    {

    return age_;

    }

    void SetAge(int age)

    {

    age_ = age;

    }

    private:

    int age_;

    };

    int _tmain(int argc, _TCHAR* argv[])

    {

    Student *student1 = new Student(STUDENTAGE);            //@test:operator -> (T *p)

    scpp::Ptr<Student> smartPoint1;

    smartPoint1 = student1;

    smartPoint1->ShowAge();

    Student *student2 = new Student(STUDENTAGE);            //@test:operator * (T *p)

    scpp::Ptr<Student> smartPoint2;

    smartPoint2 = student2;

    (*smartPoint2)。ShowAge();

    Student *student3 = new Student(STUDENTAGE);            //@test:operator * (T *p)

    scpp::Ptr<const Student> smartPoint3;

    smartPoint3 = student3;

    std::cout 《 "this student age is : " 《 smartPoint3->GetAge() 《 std::endl;

    smartPoint3->SetAge(STUDENTAGE);                     //@因为被限制,所以无法使用,也没必要使用

    return 0;

    }

        

首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇调试秘笈之运行时的错误 下一篇C++多重继承下的指针转换

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: