在容器中插入智能指针



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

#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;

有人能解释我为什么智能指针被删除后立即创建插入?

注:我不能使用共享指针和其他任何东西,我必须自己写一个。我也不能改变main

试试下面的代码:操作符=和复制构造函数的变化(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;

};

相关内容

  • 没有找到相关文章

最新更新