简体   繁体   English

struct前向声明和复制构造函数

[英]struct forward declaration and copy constructor

My abstract Reference counter class: 我的抽象参考计数器类:

template<class T>
class ReferenceCounter
{
public:

    ReferenceCounter();
    ~ReferenceCounter();

    void addRef();
    void release();

    uint32 getCountReferences() const;

protected:
    int32* pCountReferences;

    virtual void destroyObject() = 0;
    virtual void shallowCopy(const T& rhs) = 0;
};

template<class T>
inline ReferenceCounter<T>::ReferenceCounter() 
{
    pCountReferences = new int32;
    *pCountReferences = 1;
}

template<class T>
inline ReferenceCounter<T>::~ReferenceCounter() 
{
    if(pCountReferences != NULL && *pCountReferences == 0)
    {
        delete pCountReferences;
        pCountReferences = NULL;
    }
}

template<class T>
inline void ReferenceCounter<T>::addRef()
{
    debug_assert((*pCountReferences) >= 0, "Incorrect value of count references");
    ++(*pCountReferences);
}

template<class T>
inline void ReferenceCounter<T>::release()
{
    debug_assert((*pCountReferences) > 0, "Incorrect value of count references");
    (*pCountReferences)--;

    if(pCountReferences != NULL && *pCountReferences == 0)
    {
        destroyObject();
    }
}

template<class T>
inline uint32 ReferenceCounter<T>::getCountReferences() const
{
    return *pCountReferences;
}

This is my smart pointer : 这是我的聪明的指针:

template<class T>
class SmartPtr
{
public:
    SmartPtr();
    SmartPtr(T* pInst);
    SmartPtr(const SmartPtr<T>& rhs);
    ~SmartPtr();

    void operator = (const SmartPtr<T>& rhs);
    T* operator -> () const;
    T* getData() const;

    bool isNULL() const;

private:
    T* pInst;
};

template<class T>
SmartPtr<T>::SmartPtr() : pInst(NULL) {}

template<class T>
SmartPtr<T>::SmartPtr(T* pInst) : pInst(pInst) {}

template<class T>
SmartPtr<T>::~SmartPtr() 
{
    if(pInst != NULL)
    {
        pInst->release();
    }
}

template<class T>
SmartPtr<T>::SmartPtr(const SmartPtr<T>& rhs)
{
    this->pInst = rhs.pInst;
    if(pInst != NULL)
    {
        pInst->addRef();
    }
}

template<class T>
void SmartPtr<T>::operator= (const SmartPtr<T>& rhs)
{
    this->pInst = rhs.pInst;
    if(pInst != NULL)
    {
        pInst->addRef();
    }
}

template<class T>
T* SmartPtr<T>::operator->() const
{
    return pInst;
}

template<class T>
T* SmartPtr<T>::getData() const
{
    return pInst;
}

template<class T>
bool SmartPtr<T>::isNULL() const
{
    return pInst == NULL;
}

There are test of code : 有测试代码:

#include <iostream>
#include "ReferenceCounter.h"
#include "SmartPtr.h"

using namespace std;

class B;

class A : public ReferenceCounter<A>
{
public:
    A();
    A(const A& rhs);
    ~A();

    SmartPtr<B> getB();
    void operator = (const A& rhs);

    private:
    void destroyObject();
    void shallowCopy(const A& rhs);
 };

class B : public ReferenceCounter<B>
{
private:
    void destroyObject() {} ;
    void shallowCopy(const B& rhs) {};
};

A::A()
{
    cout << "Create object" << endl;
}

A::A(const A& rhs)
{
    shallowCopy(rhs);
    addRef();
    cout << "copy constructor " << endl;
}

A::~A()
{
    release();
}

void A::destroyObject()
{
    cout << "destroy" << endl;
}

void A::shallowCopy(const A& rhs)
{
    this->pCountReferences = rhs.pCountReferences;
}

void A::operator = (const A& rhs)
{
    shallowCopy(rhs);
    addRef();
    cout << "operator = " << endl;
}

SmartPtr<B> A::getB()
{
    return SmartPtr<B>(new B());
}

SmartPtr<A> getA()
{
    SmartPtr<A> a(new A());
    return a;
}

int main()
{
    getA();
    return 0;
}

This code is worked but below not called copy constructor of smart pointer when i debug this code . 这段代码有效,但在我调试此代码时,以下未称为智能指针的副本构造函数。 What problems happens below ?? 下面发生什么问题?

int main()
{
   A a;
   a.getB();
}

See here for Return value optimization . 有关返回值优化,请参见此处。

The compiler is allowed, to eliminate the copy of a temporary object being returned. 允许编译器消除返回的临时对象的副本。

With C++11, there's also the possibility of moving an object. 使用C ++ 11,还可以移动对象。 See What are move semantics? 请参阅什么是移动语义? for an explanation. 进行解释。

Update : 更新

This is not a problem at all, just a compiler optimization. 这根本不是问题,只是编译器优化。

As long as there's nothing special going on in your constructor and destructor, there's no need to prevent this optimization. 只要构造函数和析构函数中没有什么特别的事情,就无需阻止这种优化。 You should allow this instead, because it makes your program run faster by skipping one constructor and one destructor call. 您应该允许这样做,因为跳过一个构造函数和一个析构函数调用可以使程序运行更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM