void EatBeef(…);
// 可以改为
void Eat(Beef …);
void EatFish(…);
// 可以改为
void Eat(Fish …);
void EatChicken(…);
// 可以改为
void Eat(Chicken …);
|
# include <iostream.h>
void output( int x);
// 函数声明
void output( float x); // 函数声明
void output( int x)
{
cout << " output int " << x << endl ;
}
void output( float x)
{
cout << " output float " << x << endl ;
}
void main(void)
{
int x = 1;
float y = 1.0;
output(x); // output int 1
output(y); // output float 1
output(1); // output int 1
// output(0.5);
// error! ambiguous call, 因为自动类型转换
output(int(0.5)); // output int 0
output(float(0.5)); // output float 0.5
}
|
#include <iostream.h>
class Base
{
public:
void f(int x){ cout << "Base::f(int) " << x << endl; }
void f(float x){ cout << "Base::f(float) " << x << endl; }
virtual void g(void){ cout << "Base::g(void)" << endl;}
};
|
class Derived : public Base
{
public:
virtual void g(void){ cout << "Derived::g(void)" << endl;}
};
|
void main(void)
{
Derived d;
Base *pb = &d;
pb->f(42); // Base::f(int) 42
pb->f(3.14f); // Base::f(float) 3.14
pb->g(); // Derived::g(void)
}
|
#include <iostream.h>
class Base
{
public:
virtual void f(float x){ cout << "Base::f(float) " << x << endl; }
void g(float x){ cout << "Base::g(float) " << x << endl; }
void h(float x){ cout << "Base::h(float) " << x << endl; }
};
|
class Derived : public Base
{
public:
virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }
void g(int x){ cout << "Derived::g(int) " << x << endl; }
void h(float x){ cout << "Derived::h(float) " << x << endl; }
};
|
void main(void)
{
Derived d;
Base *pb = &d;
Derived *pd = &d;
// Good : behavior depends solely on type of the object
pb->f(3.14f);
// Derived::f(float) 3.14
pd->f(3.14f);
// Derived::f(float) 3.14
// Bad : behavior depends on type of the pointer
pb->g(3.14f);
// Base::g(float) 3.14
pd->g(3.14f);
// Derived::g(int) 3 (surprise!)
// Bad : behavior depends on type of the pointer
pb->h(3.14f);
// Base::h(float) 3.14 (surprise!)
pd->h(3.14f);
// Derived::h(float) 3.14
}
|
class Base
{
public:
void f(int x);
};
|
class Derived : public Base
{
public:
void f(char *str);
};
|
void Test(void)
{
Derived *pd = new Derived;
pd->f(10);
// error
}
|
#include <iostream.h>
void output( int x);
void output( int x, float y=0.0);
|
void output( int x)
{
cout << " output int " << x << endl ;
}
|
void output( int x, float y)
{
cout << " output int " << x << " and float " << y << endl ;
}
|
void main(void)
{
int x=1;
float y=0.5;
// output(x);
// error! ambiguous call
output(x,y); // output int 1 and float 0.5
}
|
运算符
|
规则
|
所有的一元运算符
|
建议重载为成员函数
|
= () [] ->
|
只能重载为成员函数
|
+= -= /= *= &= |= ~= %= >>= <<=
|
建议重载为成员函数
|
所有其它运算符
|
建议重载为全局函数
|