c++ - 无法访问同一类的私有(private)成员

标签 c++ templates c++11 g++ smart-pointers

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

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

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

下面是带有构造函数声明的模板类原型(prototype):

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

最佳答案

类(class)不同;你的构造函数是

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

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

你需要声明一个模板友元:

template<typename U> friend class Sptr;

关于c++ - 无法访问同一类的私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15922392/

相关文章:

c++ - 通过 r 值引用接收后通过引用传递

c++ - 将 unique_ptr 返回到多态类型

c++ - 以大字体打印 "HELLO"?

c++ - 如何将 Algol 68 Genia 语言转换为 C++ 语言

c++ - cucumber CPP 构建错误 : is_initialized() is not a member of unit test

c++如何创建派生类模板的实例

jquery - 使用JQuery通过id选择器从外部文件获取html内容

c++ - 确保枚举对应某种模板类型

c++ - 添加cpp11插件时报错 "Undefined reference to boost (...)"

C++11 返回值和错误对