C ++将智能指针插入容器中

时间:2014-05-25 11:50:04

标签: c++ containers smart-pointers

智能指针出现了这样的问题。我尝试将对象插入到列表中,并在创建和插入后立即调用它的构造函数。 这是主要的

#include <iostream>
#include <sstream>
#include <list>
#include "SmartPointer.h"
class Man {
    public:
        Man( const std::string& fName, const std::string& lName, int id):
            firstName(fName),lastName(lName),_id(id) { }

        ~Man( ) {
            std::cout << "Deleting, " << (std::string)*this << std::endl;
        }

        int getId( ) const {
            return _id;
        }

        operator std::string( ) {
            std::ostringstream oss;
            oss << "First Name: " << firstName << ", "
                << "Last Name: " << lastName << ", "
                << "ID number: " << _id << ";";
                return oss.str();
        }
    private:
        std::string firstName;
        std::string lastName;
        int _id;
};

class deleteByNumber {
    public:
        deleteByNumber( int number): _number(number) { }
        bool operator( )(SmartPointer<Man>& someGuy) const {
            return someGuy->getId( ) == this->_number;
        }
    private:
        int _number;
};

int main( ) {
    std::list<SmartPointer<Man> > courses;
    courses.push_back( SmartPointer<Man>( new Man( "Alexander", "Great", 7777)));
    courses.push_back( SmartPointer<Man>( new Man( "Brad", "Pitt", 2244555)));
    SmartPointer<Man> p = SmartPointer<Man>( new Man( "Mel", "Gibson", 333));
    courses.push_back( p);
    courses.push_back( SmartPointer<Man>( new Man( "Sigmund", "Freud", 55334)));
    courses.remove_if( deleteByNumber(2244555));
    p = SmartPointer<Man>( new Man( "David", "Nil", 12678));
    courses.remove_if( deleteByNumber(333));
    courses.push_back( SmartPointer<Man>( new Man( "David", "Nil", 12678)));
    courses.push_back( SmartPointer<Man>( new Man( "Andy", "Warhol", 16701)));
    return 1;
}

这是一个智能指针

#ifndef _SMART_POINTER_H_
#define _SMART_POINTER_H_


#include<iostream>

template<class T>
class SmartPointer{
public:

    //default c'tor
    SmartPointer() : object(NULL), counter(new int){
        *counter = 1;
    }


    //c'tor
    SmartPointer(T* p) : object(p), counter(new int){
        *counter = 1;
    }


    //copy c'tor
    SmartPointer(const SmartPointer<T>& sp): object(sp.object){
        counter=sp.counter;
        *counter++;
    }

    //d'tor
    ~SmartPointer(){
        if((*counter)==1){
            delete counter;
            delete object;
        }
        else{
            (*counter)--;
        }

    }

    //assignement operator =
    SmartPointer<T>& operator=(const SmartPointer<T> sp){

        if(this != &sp){             // checks that its not a self assignement
            if(--(*counter)==0){       // this object counter will no longer point to the same place so we need to decrese it.
                delete counter;         // if he is the only one pointing there we should delete it.
                delete object;
            }
            object=sp.object;
            counter=sp.counter;
            *counter++;
        }
        return *this;
    }




    T& operator*(){
        return *object;
    }

    T* operator->(){
        return object;
    }

private:
    T* object;
    int* counter;


};


#endif

这是输出:

Deleting, First Name: Alexander, Last Name: Great, ID number: 7777;

Deleting, First Name: Brad, Last Name: Pitt, ID number: 2244555;

Deleting, First Name: Sigmund, Last Name: Freud, ID number: 55334;

Deleting, First Name: Mel, Last Name: Gibson, ID number: 333;

Deleting, First Name: David, Last Name: Nul, ID number: 12678;

Deleting, First Name: David, Last Name: Nil, ID number: 12678;

Deleting, First Name: Andy, Last Name: Warhol, ID number: 16701;

有人可以解释为什么在创建\插入后立即删除智能指针吗?

P.S。我不能使用共享指针和其他任何东西,我必须自己编写。而且我也无法改变主要的

1 个答案:

答案 0 :(得分:0)

试试这段代码: operator =和copy构造函数的变化(use(* counter)++;) 如果你想使用列表中的智能指针,那么可能只移动语义就足够了。

template<class T>
class SmartPointer{
public:

    //default c'tor
    SmartPointer() : object(NULL), counter(new int){
        *counter = 1;
    }


    //c'tor
    SmartPointer(T* p) : object(p), counter(new int){
        *counter = 1;
    }


    //copy c'tor
    SmartPointer(const SmartPointer<T>& sp): object(sp.object){
        counter=sp.counter;
        (*counter)++;
    }

    //d'tor
    ~SmartPointer(){
        if((*counter)==1){
            delete counter;
            delete object;
        }
        else{
            (*counter)--;
        }

    }

    //assignement operator =
    SmartPointer<T>& operator=(const SmartPointer<T> sp){

        if(this != &sp){             // checks that its not a self assignement
            if(--(*counter)==0){       // this object counter will no longer point to the same place so we need to decrese it.
                delete counter;         // if he is the only one pointing there we should delete it.
                delete object;
            }
            object=sp.object;
            counter=sp.counter;
            (*counter)++;
        }
        return *this;
    }




    T& operator*(){
        return *object;
    }

    T* operator->(){
        return object;
    }

private:
    T* object;
    int* counter;


};