#include <iostream>
#include <conio.h>
using namespace std;
template <typename type> void show(); //模板函数声明
template <typename type> void sshow( type &);
template <typename type > //类模板定义
class Aclass
{
private:
type val;
static int vv;
public:
Aclass ():val(97){++vv;}
friend void show<type> (); //声明模板为友元
friend void sshow<>( Aclass<type> &,int );
};
template <typename T> //静态变量初始化
int Aclass<T>::vv = 0;
template <typename T > //show定义
void show ()
{
cout<< Aclass<T>::vv <<endl; //★而这里却可以访问vv?
}
template <typename T > //sshow定义
void sshow ( T & a,int b )
{
// Aclass<int>::vv; //★问题在这里,为什么不能这样访问静态变量vv?
cout<< a.val<<endl;
cout<<"val="<<b<<endl;
}
int main()
{
Aclass<double> bb;
sshow(bb ,7.3 );
getch();
return 0;
}
友元的作用域应该都是整个类啊,为什么不能访问,求解
#include <conio.h>
using namespace std;
template <typename type> void show(); //模板函数声明
template <typename type> void sshow( type &);
template <typename type > //类模板定义
class Aclass
{
private:
type val;
static int vv;
public:
Aclass ():val(97){++vv;}
friend void show<type> (); //声明模板为友元
friend void sshow<>( Aclass<type> &,int );
};
template <typename T> //静态变量初始化
int Aclass<T>::vv = 0;
template <typename T > //show定义
void show ()
{
cout<< Aclass<T>::vv <<endl; //★而这里却可以访问vv?
}
template <typename T > //sshow定义
void sshow ( T & a,int b )
{
// Aclass<int>::vv; //★问题在这里,为什么不能这样访问静态变量vv?
cout<< a.val<<endl;
cout<<"val="<<b<<endl;
}
int main()
{
Aclass<double> bb;
sshow(bb ,7.3 );
getch();
return 0;
}
友元的作用域应该都是整个类啊,为什么不能访问,求解
