我正在写一个轻松“打开”模式的课程,然后我有点困惑。在重载 operator= 从另一个类复制时,授予对私有T
成员m_Mode
的访问权限,为什么会发生这种情况?它是标准的,还是编译器错误?
template<class T>
class CFixedMode
{
private:
T m_Mode;
public:
CFixedMode()
{
m_Mode = static_cast<T>(0);
}
~CFixedMode(){}
void SetMode( T mode );
void SetNotMode( T mode );
BOOL IsMode( T mode );
CFixedMode<T>& operator=( const CFixedMode< T >& rFixedMode );
};
template<class T>void CFixedMode<T>::SetMode( T mode )
{
m_Mode |= mode;
}
template<class T>void CFixedMode<T>::SetNotMode( T mode )
{
m_Mode &= (~mode);
}
template<class T>BOOL CFixedMode<T>::IsMode( T mode )
{
return ( ( m_Mode & mode ) == mode ) ? TRUE : FALSE;
}
template<class T>CFixedMode<T>& CFixedMode<T>::operator =( const CFixedMode<T>& rFixedMode )
{
if( typeid( m_Mode ) == typeid( rFixedMode.m_Mode ) )
m_Mode = rFixedMode.m_Mode;
return *this;
}
int _tmain(int argc, _TCHAR* argv[])
{
CFixedMode<DWORD> Mode;
DWORD rMode = 0x00000010;
Mode.SetMode( rMode );
CFixedMode<DWORD> Mode2;
Mode2 = Mode;
if( Mode2.IsMode( 0x00000010 ) )
{
//cout << Mode2.m_Mode; //c2248
cout << "True" << endl;
}
typeid(Mode).before(typeid(CFixedMode<DWORD>));
return 0;
}