我自己编程遇到了点问题,一个模板类的模板构造函数访问不了参数的私有变量......
这是简化的说明代码:
#include<iostream>
using namespace std;
template<typename T>
class A{
public:
A(T xs){
x=xs;
}
template<typename U>
A(const A<U>& xs){
x=xs.x;
}
print(){
cout<<x<<endl;
}
private:
T x;
};
int main(){
A<char> a('x');
A<int> b(a);
b.print();
return 0;
}
编译会报错:
main.cpp In instantiation of 'A::A(const A&) [with U = char; T = int]':
22 12 main.cpp required from here
18 5 main.cpp [Error] 'char A::x' is private
11 5 main.cpp [Error] within this context
可是明明是同一个类,为什么不能访问private...
哪位大佬给讲讲呗