游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2536|回复: 6

继承vs的ostream的苦恼+混乱的函数匹配?

[复制链接]

30

主题

276

帖子

276

积分

中级会员

Rank: 3Rank: 3

积分
276
发表于 2004-8-3 16:27:00 | 显示全部楼层 |阅读模式
本来我想通过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这个优先级匹配也符合标准么?

1万

主题

1万

帖子

2万

积分

管理员

中级会员

Rank: 9Rank: 9Rank: 9

积分
20547
发表于 2004-8-3 20:30:00 | 显示全部楼层

Re:继承vs的ostream的苦恼+混乱的函数匹配?

汗!从来没有想过要继承ostream,感觉很少用到继承标准库的内容,呵呵,不错的想法,不过估计处理起来会相当麻烦,楼主的问题,只能帮顶关注了!

1

主题

66

帖子

78

积分

注册会员

Rank: 2

积分
78
发表于 2004-8-4 13:18:00 | 显示全部楼层

Re:继承vs的ostream的苦恼+混乱的函数匹配?

感觉c++的模板参数匹配过于繁琐,以后的版本是不是应该改一改。。。

7

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
QQ
发表于 2004-8-4 13:24:00 | 显示全部楼层

Re:继承vs的ostream的苦恼+混乱的函数匹配?

你要用全局的话可以这样吧:
std:perator<<(* this,a_data);

30

主题

276

帖子

276

积分

中级会员

Rank: 3Rank: 3

积分
276
 楼主| 发表于 2004-8-4 14:54:00 | 显示全部楼层

Re:继承vs的ostream的苦恼+混乱的函数匹配?

如果指定用成员函数来调用全局会出现这个问题啊
MS用全局函数就是解决这个问题 :(
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.

最讨厌的是都VS2003了,还是不支持模版的模版参数 。

30

主题

276

帖子

276

积分

中级会员

Rank: 3Rank: 3

积分
276
 楼主| 发表于 2004-8-4 14:55:00 | 显示全部楼层

Re:继承vs的ostream的苦恼+混乱的函数匹配?

这个问题可能真的是继承无解的吧?

30

主题

276

帖子

276

积分

中级会员

Rank: 3Rank: 3

积分
276
 楼主| 发表于 2004-8-4 15:31:00 | 显示全部楼层

Re:继承vs的ostream的苦恼+混乱的函数匹配?

其实问题的根源是
C++里面 派生类 同名函数 自动覆盖基类函数这个规则 。
有了这个规则
比如基类这样
class a
{
operator<<(int)  
operator<<(char)
operator<<(short)
operator<<(float)
}
假如你想对 fuc(int) 做一些特化处理 。
你想这样
class b :public a
{
operator<<(int);
}
那你就不能再使用 operator<<(char) operator<<(short)
operator<<(float) 。 。 。
所以??
或许你可以这样
class b :pubic a
{
operator<<(int);
template <class T>
operator<<(T arg);
}
但是 如果 a定义了很多这样的全局函数
operator<<(a arg,unsigned int);
. . .
那又怎样?
也许
class b :pubic a
{
operator<<(int);
operator<<(unsigned int) ;
. . .
template <class T>
operator<<(T arg);
}
那得到的类的匹配优先级,就被更改了,fuc(unsigned int)由原来的
全局优先级提到了成员函数(VS的那个诡异的理由不列举,手工复制那么多函数也不计),而且 基类 多添加一个 全局函数后 ,忽然你的派生类就出错了。

这个问题好象只有用组合才能比较幽雅的解决

a m_a

operator<<(int arg)
{
m_a<<arg;

}
template <class T>
operator<<(T arg)
{
  m_a<<arg;
}

这个就是MS那个ostream问题的一个简化版的说明。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-16 11:34

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表