|
本来我想通过VS的STL的ofstream做一个带有start,end开关的
safe_ofstream出来,就是我调用start时safe_ofstream才向文件写东西
调用end的时候safe_ofstream就不向文件里写东西了.
说白了就是要想在safe_ofstream里这样...
operator<<(T)
{
if(start)write
.....
}
虽然这个前人说过,在派生类里只要需要重定义基类函数就不应该用使用继承(虚除外),但是本着想少写一点代码的原则,我还是继了
结果 . . .
----------------------
上面是废话,下面是实际问题
----------------------
因为vs的stl的ostream实作的<<有很多是全局的
他们的理由是
This explicit template specialisation writes a single unicode character to the stream.
By default, the compiler treats wchar_t as if it were a typedef for unsigned short. This means that we cannot distinguish between an unsigned short and a wchar_t in the library. To be most consistent with previous practice, we add this explicit specialisation to ensure that a single unsigned short is read and written as a character.
所以继承的时候
template <typename T>
safe_ofstream& operator<<(T a_data)
{
if(start){ofstream: perator<<(a_data) ;}
return *this;
}
的话就会调用不到ostream的全局<<
。 。 。 。 。 。 。 。 。。 。 。 。。 。 。 。 。 。 。
若是将ofstream的成员<<全部覆盖一遍的话,则会因为函数匹配出错
比如我覆盖了 成员operator<<(int) 当读取char时
就会有6个函数的匹配优先级一致.
safe_ofstream<_Elem,_Traits> &safe_ofstream<_Elem,_Traits>::operator <<(int) ;
std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)
std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)
std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,_Elem)
std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)
std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)
。 。 。 。 。这段话有点逻辑错误,只是表明VS的匹配规律.
有谁能把这个东西继承下来的吗?有的话,清给我讲解一下,谢谢.
反正我是继承不动了 这再次印证了effort c++的老话 , 继承是不能
覆盖非虚基类函数的.如果我说话有点混乱大家不要骂我,整个下午都在
计算这个优先级,怎么知道就是继承不过去 
不知道标准的stl有没有这样的问题?
MS这个优先级匹配也符合标准么? |
|