{
m_StackSize = st.m_StackSize;
m_nTop = st.m_nTop;
items = new Type[ st.m_StackSize ];
for ( int i = 0; i < m_StackSize; i++ )
{
items[ i ] = st.items[ i ];
}
}
template
bool CStack
{
if ( !isFull() )
{
items[ m_nTop ] = Item;
m_nTop++;
return true;
}
return false;
}
template
bool CStack
{
/*
if ( !isEmpty() )
{
Item = items[ m_nTop ]; // 注意这样写是不对的,因为未满的时候items[ m_nTop ]指向未知
m_nTop--;
return true;
}*/
if ( m_nTop > 0)
{
Item = items[ --m_nTop ];
return true;
}
return false;
}
template
CStack
{
if ( rCStack == *this)
{
return *this;
}
if ( items )
{
delete[] items;
}
m_StackSize = st.m_StackSize;
m_nTop = st.m_nTop;
items = new Type[ st.m_StackSize ];
for ( int i = 0; i < m_StackSize; i++ )
{
items[ i ] = st.items[ i ];
}
}
template< template
class Crab
{
public:
Crab(){}
bool push( int iA, double fB )
{
return ( s1.push( iA ) && s2.push( fB ) );
}
bool pop( int& iA, double& fB )
{
return ( s1.pop( iA ) && s2.pop( fB ) );
}
private:
Thing
Thing
};
int _tmain(int argc, _TCHAR* argv[])
{
Crab
int nj;
double nb;
cout << "Enter int double pairs, such as 4 3.5 ( 0 0 to be end):" << endl;
while ( cin >> nj >> nb && nj > 0 && nb > 0 )
{
if ( !nebula.push( nj, nb ) )
{
break;
}
}
while ( nebula.pop( nj, nb) )
{
cout << nj << ", " << nb << endl;
}
cout << "Done." << endl;
return 0;
}
在这个类里还可以使用模板参数和常规参数,例如:
template< template
class Crab
{
private:
Thing s1;
Thing
}
模板类和友元
模板类声明也可以有友元。模板的友元分3类:
1.非模板友元
[cpp]
// testTemplateFriend2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
using namespace std;
#include "string"
template
class CHasFriend
{
public:
CHasFriend( const T& t ) : item( t )
{
ct++;
}
~CHasFriend()
{
ct--;
}
friend void counts();
friend void report( const CHasFriend
private:
explicit CHasFriend( const CHasFriend& rCHasFriend ){}
private:
T item;
static int ct;
};
template
int CHasFriend
void counts()
{
cout << "int count:" << CHasFriend
cout << " double count:" << CHasFriend
}
void report( const CHasFriend
{
cout << "CHasFriend item:" << hf.item << endl;
}
void report( const CHasFriend
{
cout << "CHasFriend item:" << hf.item << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << "No objects declared:";
counts();
CHasFriend
cout << "After hfil1 declared:";
counts();
CHasFriend
cout << "After hfil2 declared:";
counts();
CHasFriend
cout << "After hfdb declared:";
counts();
report( hfil1 );
report( hfil2 );
report( hfdb );
return 0;
}
代码中声明使counts()函数成为模板所有实例化的友元。它内部的CHasFriend
2.约束(bound)模板友元,即友元的类型取决于类呗实例化时的类型
[cpp]
// testTemplateFriend3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
using namespace std;
#include "string"
template
void counts()
{
cout << "this count value is:" << CHasFriend
}
template
void report( const T& hr )
{
cout << "this count value is:" << hr.m_item << endl;
}
template
class CHasFriend
{
public:
CHasFriend( const TT item ) : m_item( item )
{
ct++;
}
~CHasFriend()
{
ct--;
}
friend void counts();
friend void report<>( const CHasFriend& hr );
private:
explicit CHasFriend( const CHasFriend& rCHasFriend ){}
private:
TT m_item;
static int ct;
};
template
int CHasFriend