Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My question is about the copy constructor and operator = overload in this code. I uderstand it but not perfectly. Can someone explain me that two expressions? Thanks in advance!!!
C++
#include <iostream>
using namespace std;

template <class T>

class Auto_ptr2{
private:
    T *m_ptr;
public:
    Auto_ptr2(T *ptr=nullptr)
    {


    }
    ~Auto_ptr2(){
    delete m_ptr;
    }
    Auto_ptr2(Auto_ptr2 &p)
    {
        m_ptr=p.m_ptr;
        p.m_ptr=nullptr;


    }
    Auto_ptr2 operator=(Auto_ptr2 &p)
    {
        if(&p==this){return *this;}
        else{
            delete m_ptr;
            m_ptr=p.m_ptr;
            return *this;

        }

    }
    T &operator*(){return *m_ptr;}
    T *operator->(){return m_ptr;}
bool isNull()const{return m_ptr==nullptr;}
};

class Resource{
public:
    Resource(){cout<<"Resource is acquired"<<endl;}
    ~Resource(){cout<<"Resource is destroyed"<<endl;}


};


int main()
{
    Auto_ptr2<Resource> res1(new Resource);
    Auto_ptr2<Resource> res2;
    cout<<"res1 is: "<<(res1.isNull()?"null":"not null")<<endl;
    cout<<"res2 is: "<<(res2.isNull()?"null": "not null")<<endl;
    res2=res1;
    cout<<"res1 is: "<<(res1.isNull()?"null":"not null")<<endl;
    cout<<"res2 is: "<<(res2.isNull()?"null": "not null")<<endl;


}


What I have tried:

I have tried to understand it, I understand it now, but not perfectly :)
Posted
Updated 25-Jun-17 1:49am
Comments
Philippe Mori 25-Jun-17 7:45am    
Standard auto_ptr class has known problems... so your code based on its implementation. If you have a modern C++ compiler, better to use unique_ptr or shared_ptr instead.

In the copy and constructor are assigments of the member pointer, which contains the raw pointer. But these arent not only pointers, but also allocated memory which must get handled seperatly.

I would judge them a wrong, by not making a deep copy but deleting the right side.

My implementation
C++
Auto_ptr2(const Auto_ptr2 &p)
{
    m_ptr= MakeCopy( p.m_ptr );//make a full copy of object
    //p.m_ptr=nullptr;
}
Auto_ptr2 operator=(const Auto_ptr2 &p)
{
    if(&p==this){return *this;}
    else{
        delete m_ptr;//correct deleting the old raw copy
        m_ptr=MakeCopy( p.m_ptr );
        return *this;
    }
}

Another solution can be the so called reference counting architecture, when a counter is used to controls the lifecycle of the raw pointer data.
 
Share this answer
 
Essentially auto_ptr was an attempt to move ownership before C++ 11. However, that implementation has known problems because the language at that time didn't yet have rvalue reference and move semantic...

If possible use std::unique_ptr or std::shared_ptr.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900