1

我正在实现一个智能指针类作为练习。考虑以下:

class Base1 {
    protected:
        Base1() : derived_destructor_called(false) {
            printf("Base1::Base1()\n");
        }
    private:
        Base1(const Base1 &); // Disallow.
        Base1 &operator=(const Base1 &); // Disallow.
    protected:
        ~Base1() {
            printf("Base1::~Base1()\n");
            assert(derived_destructor_called);
        }
    protected:
        bool derived_destructor_called;
};

class Derived : public Base1 {
        friend void basic_tests_1();
    private:
        Derived() {printf("Derived::Derived()\n");}
        Derived(const Derived &); // Disallow.
        Derived &operator=(const Derived &); // Disallow.
    public:
        ~Derived() {
            printf("Derived::~Derived()\n");
            derived_destructor_called = true;
        }
        int value;
};

以及以下测试代码:

            Sptr<Derived> sp(new Derived);
            // // Test template copy constructor.
            Sptr<Base1> sp3(sp);

产生以下错误:

Sptr.cpp: In instantiation of ‘my::Sptr<T>::Sptr(const my::Sptr<U>&) [with U = Derived; T = Base1]’:
Sptr.cpp:272:35:   required from here
Sptr.cpp:41:6: error: ‘Derived* my::Sptr<Derived>::obj’ is private
Sptr.cpp:117:86: error: within this context
Sptr.cpp:42:13: error: ‘my::RC* my::Sptr<Derived>::ref’ is private
Sptr.cpp:117:86: error: within this context
Sptr.cpp:43:31: error: ‘std::function<void()> my::Sptr<Derived>::destroyData’ is private
Sptr.cpp:117:86: error: within this context

这怎么可能?我指的是同一个类中的相同类变量。!

下面是模板类原型及其构造函数声明:

template <class T>
class Sptr {
private:
    //some kind of pointer
        //one to current obj
    T* obj;
    RC* ref;
    std::function<void()> destroyData;
public:
    Sptr();
    ~Sptr();

    template <typename U> 
    Sptr(U *);

    Sptr(const Sptr &);

    template <typename U> 
    Sptr(const Sptr<U> &);

    template <typename U> 
    Sptr<T> &operator=(const Sptr<U> &);

    Sptr<T> &operator=(const Sptr<T> &);

    void reset();

    T* operator->() const
    {return obj;};

    T& operator*() const
    {return *obj;};

    T* get() const
    {return &obj;};

    //operator unspecified_bool_type() const;

    //overload *,->,=,copy-constructor

    // const-ness should be preserved.
    // Test for null using safe-bool idiom
    // Static casting, returns a smart pointer
};

编辑

我在模板类中声明了一个模板朋友,如下所示:

template<typename U> friend class Sptr;

这样就解决了上面的错误。但它产生了新的错误!

测试代码:

        Sptr<Base1> sp2;
        {
            Sptr<Derived> sp(new Derived);
            // // Test template copy constructor.
            Sptr<Base1> sp3(sp);
            sp2 = sp;
            sp2 = sp2;
        }

错误:

Sptr.cpp: In instantiation of ‘my::Sptr<T>& my::Sptr<T>::operator=(const my::Sptr<U>&) [with U = Derived; T = Base1]’:
Sptr.cpp:274:23:   required from here
Sptr.cpp:128:9: error: comparison between distinct pointer types ‘my::Sptr<Base1>*’ and ‘const my::Sptr<Derived>*’ lacks a cast
Sptr.cpp:212:9: error: ‘Base1::~Base1()’ is protected
Sptr.cpp:132:21: error: within this context
Sptr.cpp: In instantiation of ‘my::Sptr<T>& my::Sptr<T>::operator=(const my::Sptr<T>&) [with T = Base1]’:
Sptr.cpp:275:23:   required from here
Sptr.cpp:212:9: error: ‘Base1::~Base1()’ is protected
Sptr.cpp:154:21: error: within this context

当我已经将它设为模板朋友时,这怎么可能?= 运算符下面重载

template <typename T> 
Sptr<T>& Sptr<T>::operator=(const Sptr<T> &t) {

    std::cout<<"= const <T>\n";
    if(this != &t) {
        if(ref->Release() == 0) {

            if(obj)
                delete obj;

            delete ref;       
        }

        obj = t.obj;
        ref = t.ref;
        destroyData = t.destroyData;
        ref->AddRef();
    }

    return *this;
}
template <typename T> 
template <typename U> 
Sptr<T>& Sptr<T>::operator=(const Sptr<U> &t) {

    std::cout<<"= const <U>\n";
    if(this != &t) {
        if(ref->Release() == 0) {

            if(obj)
                delete obj;

            delete ref;       
        }

        obj = t.obj;
        ref = t.ref;
        destroyData = t.destroyData;
        ref->AddRef();
    }

    return *this;
}
4

3 回答 3

4

班级不同;你的构造函数是

my::Sptr<Base1>::Sptr(const my::Sptr<Derived>&)

并且您正在尝试访问my::Sptr<Derived>.

您需要声明一个模板朋友:

template<typename U> friend class Sptr;
于 2013-04-10T09:53:50.233 回答
4

我指的是同一个类中的相同类变量。

不你不是。也许你指的是同一个类模板的相同成员,我猜是这样的

    template <typename U> 
    Sptr(const Sptr<U> & other) : obj(other.obj), ref(other.ref) {}

但是您没有访问同一个的成员,因为Sptr<Base>Sptr<Derived>是模板的不同实例,因此是不同的类,不能触及彼此的隐私。

更新您的编辑:您现在得到的错误与访问权限无关,但它再次与不同的Sptr<Base>类有关Sptr<Derived>,因此指向它们的指针是无法比较的不同指针类型。
您显示的代码operator=适用于相同的模板实例化 ( Sptr<T>),但错误发生在不同模板实例化 ( Sptr<U>) 的运算符中。我猜你在那里也有地址比较,这是没有必要的,因为 aSptr<T>不能与 a 具有相同的地址Sptr<U>

关于 protected 的第二个错误~Base是显而易见的:您将~Baseprotected 和非虚拟化,意味着您不会Base通过删除Base*指针来破坏对象。这同样适用于指向 的智能指针Base,因为它们会使用您禁止的多态删除。解决方案:使~Base公共和虚拟化。

于 2013-04-10T09:54:03.370 回答
2

他们不是同一个班级。看起来它在抱怨您试图访问Sptr<Derived>from的成员Sptr<Base1>- 查看错误消息开头的类型变量实例化。

模板,当它们的类型变量被填充时,会生成全新的类,这些类恰好看起来非常相似,因此 aMyClass<string>与 a 不同MyClass<vector<int>>

于 2013-04-10T09:54:40.497 回答