64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
template <typename T>
class Array;
template<typename T>
void f( const Array<T> &a );
template <typename T>
class Array
{
T* array;
int size;
public:
Array(int sz): size(sz)
{
size = sz;
}
friend void f<> ( const Array<T> &a );
//为什么不能在类中定义友元函数呢??
//很奇怪的是, 当我把友元函数 f<>的specialization的定义
// template<>
// void f( const Array<int> &a)
// {
// cout << a.size << " int" << endl;
// }
//注释之后, 下面的定义能够通过编译, 并正确运行
template<typename Type>
friend void testVer2( const Array<Type> &a )
{
cout<< "what" << endl;
}
};
// f function template, friend of Array<T>
template <class T>
void f( const Array<T> &a)
{
cout << a.size << " generic" << endl;
}
// Specialization of f for char arrays
// will be a friend because the template f is a friend.
template<>
void f( const Array<int> &a)
{
cout << a.size << " int" << endl;
}
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
template <typename T>
class Array;
template<typename T>
void f( const Array<T> &a );
template <typename T>
class Array
{
T* array;
int size;
public:
Array(int sz): size(sz)
{
size = sz;
}
friend void f ( const Array<T> &a )
{
cout << a.size << " int" << endl;
}
//为什么不能在类中定义友元函数呢??
//很奇怪的是, 当我把友元函数 f<>的specialization的定义
// template<>
// void f( const Array<int> &a)
// {
// cout << a.size << " int" << endl;
// }
//注释之后, 下面的定义能够通过编译, 并正确运行
template<typename Type>
friend void testVer2( const Array<Type> &a )
{
cout<< "what" << endl;
}
};
// f function template, friend of Array<T>
template <class T>
void f( const Array<T> &a)
{
cout << a.size << " generic" << endl;
}
// Specialization of f for char arrays
// will be a friend because the template f is a friend.
template<>
void f( const Array<int> &a)
{
cout << a.size << " int" << endl;
}
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
template <typename T>
class Array;
template<typename T>
void f( const Array<T> &a );
template <typename T>
class Array
{
T* array;
int size;
public:
Array(int sz): size(sz)
{
size = sz;
}
friend void f<> ( const Array<T> &a );
//为什么不能在此处定义友元函数呢??
//很奇怪的是, 当我把友元函数 f<>的specialization的定义
// template<>
// void f( const Array<int> &a)
// {
// cout << a.size << " int" << endl;
// }
//注释之后, 下面的定义能够通过编译, 并正确运行
template<typename Type>
friend void testVer2( const Array<Type> &a )
{
cout<< "what" << endl;
}
};
// f function template, friend of Array<T>
template <class T>
void f( const Array<T> &a)
{
cout << a.size << " generic" << endl;
}
// Specialization of f for char arrays
// will be a friend because the template f is a friend.
//template<>
//void f( const Array<int> &a)
//{
// cout << a.size << " int" << endl;
//}
int main( void )
{
Array<char> chArr(4);
testVer2( chArr );
return 0;
}