64,648
社区成员
发帖
与我相关
我的任务
分享
template<class T>
class Test{
private:
T n;
const T i;
static int cnt;
public:
Test():i(0){n=0;}
Test(T k);
~Test(){}
void print();
T operator+(T& x);};
template<class T>
int Test<T>::cnt=0;
template<class T>
Test<T>::Test(T k):i(k){n=k;cnt++;}
template<class T>
void Test<T>::print(){
std::cout<<"n="<<n<<std::endl;
std::cout<<"i="<<i<<std::endl;
std::cout<<"cnt="<<cnt<<std::endl;
}
template<class T>
T Test<T>::operator+(T& x){
return n + x;
}
#include <iostream>
using namespace std;
template<class T>
class Test
{
private:
T n;
const T i;
static int cnt;
public:
Test():i(0){n=0;}
Test(T k);
~Test(){}
void print();
T operator+(T& x);
};
template<class T>
int Test<T>::cnt=0;
template<class T>
Test<T>::Test(T k):i(k)
{
n=k;cnt++;
}
template<class T>
void Test<T>::print()
{
std::cout<<"n="<<n<<std::endl;
std::cout<<"i="<<i<<std::endl;
std::cout<<"cnt="<<cnt<<std::endl;
}
template<class T>
T Test<T>::operator+(T& x)
{
return n + x;
}
int main()
{
int i=1;
Test<int> a(1);
cout<<a+i<<endl;
return 0;
}
template<class T>
class Test
{
private:
T n;
const T i;
static int cnt;
public:
Test():i(0){n=0;}
Test(T k);
~Test(){}
void print();
T operator+(T& x);
};
template<class T>
int Test<T>::cnt=0;
template<class T>
Test<T>::Test(T k):i(k)
{
n=k;cnt++;
}
template<class T>
void Test<T>::print()
{
std::cout<<"n="<<n<<std::endl;
std::cout<<"i="<<i<<std::endl;
std::cout<<"cnt="<<cnt<<std::endl;
}
template<class T>
T Test<T>::operator+(T& x)
{
return n + x;
}
int main()
{
return 0;
}
#include <iostream>
using namespace std;
template<class T>
class Test{
private:
T n;
const T i;
static int cnt;
public:
Test():i(0){n=0;}
Test(T k);
~Test(){}
void print();
T operator+(const T& x);//用const
};
template<class T>
int Test<T>::cnt=0;
template<class T>
Test<T>::Test(T k):i(k){n=k;cnt++;}
template<class T>
void Test<T>::print()
{
std::cout<<"n="<<n<<std::endl;
std::cout<<"i="<<i<<std::endl;
std::cout<<"cnt="<<cnt<<std::endl;
}
template<class T>
T Test<T>::operator+(const T& x){//用const
return n + x;
}
int main(void)
{
Test<int> a(1);
cout << a+1 << endl;
return 0;
}