- 论坛徽章:
- 0
|
在c++编程思想看到的一个例子
#include <iostream>
using std::cout;
using std::endl;
class F
{
int i ;
public:
F(int a){i=a;}
friend void f(const F&);
void g()
{
f(*this);
}
/*ostream& operator<<(cout,const F& f)
{
cout<<f.i<<"---------"<<endl;
return cout;
}*/
};
void h()
{
f(F(1));//正确
//f(1); 错误
1)f(F(1))中的F(1)解析后,f()函数本山怎么找到的呢,因为它定义在下面了?
2)既然能正确找到,上面的f(F(1))写成f(1)为什么不行呢,因为1可以转换到F类型的?
3)若把f()的定义放在h()定义的前面,经测试,f(1)和f(F(1))就都正确了。
}
void f(const F& f)
{
cout<<f.i<<endl;
}
int main()
{
h();
F(2).g();
return 0;
}
环境
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
[ 本帖最后由 FightingWu 于 2007-11-30 18:52 编辑 ] |
|