开发者

Member function cannot access private member

开发者 https://www.devze.com 2023-01-06 21:31 出处:网络
I have got the following code #include <iostream> #include <string> template <typename T>

I have got the following code

#include <iostream>
#include <string>

template <typename T>

class demo
{
    T data;
  public:
    demo();
    demo(demo const&k );
    demo(const T&k);
    demo& operator=(const demo &k);

    template<typename T1>
    demo(const demo<T1>&k);

    template<typename T1>
    demo<T>& operator=(const demo<T1>&k);

   ~demo();
};

template <typename T>
demo<T>::demo():data(){}

template<typename T>
demo<T>::demo(demo const& k):data(k.data){}

template<typename T>
demo<T>::demo(const T&k):data(k){}

template<typename T>
demo<T>&am开发者_如何学JAVAp; demo<T>::operator=(demo const &k)
{
    if ((void*)this == (void*)&k) {    // assignment to itself? 
         return *this; 
        } 
    this->data=k.data;
    return *this;
}

template<typename T>
  template<typename T1>
  demo<T>& demo<T>::operator=(demo<T1> const &k)
  {
    if ((void*)this == (void*)&k) {    // assignment to itself? 
         return *this; 
        } 

    this->data=k.data;
    return *this;
  }

template<typename T>
  template<typename T1>
  demo<T>::demo(const demo<T1>&k):data(k.data){}

template<typename T>
demo<T>::~demo(){}

int main()
{
    demo<std::string> k(std::string("hello"));
    demo<std::string >l=k;
    demo<int> x(10);
    demo<double> p=x; //error here
}

Why do I get error here? As far as I know p is being copy initialized to x. So

 demo<T>::demo(const demo<T1>&k):data(k.data){} 

is invoked. But since data is a private member I get an error 'demo<T>::data' : cannot access private member declared in class 'demo<T>'. Why?

I know that member functions of a class can access private members , so why do I get an error? What should I do to correct the error?


Because demo<T> and demo<T1> are considered different types they may not access each other's private data.

Easist way to solve this is by adding a public accessor function and using it:

template <typename T>
class demo
{
public:
    const T &get_data() const { return data; }
    ...
};

template<typename T>
template<typename T1>
demo<T>::demo(const demo<T1>&k):data(k.get_data()){}


As answered by R Samuel Klatchko before me, demo<T> and demo<T1> are not of the same types.

The solution to your problem would be to declare the other classes friend:

class demo
{
    T data;
  public:
    demo();
    demo(demo const&k );
    demo(const T&k);
    demo& operator=(const demo &k);

    template<typename T1>
    demo(const demo<T1>&k);

    template<typename T1>
    demo<T>& operator=(const demo<T1>&k);

   ~demo();

    template<typename T1>   // This will enable demo<T> and demo<T1>
    friend class demo ;     // to see each other's privates as friends
                            // ought to do...
};
0

精彩评论

暂无评论...
验证码 换一张
取 消