对于effective c++ 条款33的转交函数有疑问。先描述一下前面所说的内容作为铺垫:
C++中函数的局部作用域会遮掩更大作用域中的同名的变量和函数。在局部作用域中,名称查找顺序为:局部作用域-外围更高一级的作用域-更外围作用域-全局作用域。
在继承体系下,派生类的作用域包含在基类作用域下,因而在派生类中的同名变量会遮掩基类的变量。
直接看代码:
#include <iostream>
using namespace std;
class Base
{
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int ) { cout << "call Base::mf1(int )" << endl; }
virtual void mf2() { cout << "call Base::mf2()" << endl; }
void mf3() { cout << "call Base::mf3()" << endl; }
void mf3(double ) { cout << "call Base::mf3(double )" << endl; }
};
class Derived: public Base
{
public:
virtual void mf1() { cout << "call Derived::mf1()" << endl; }
void mf3() { cout << "call Derived::mf3()" << endl; }
void mf4() { cout << "call Derived::mf4()" << endl; }
};
int main(void)
{
Derived d;
int x = 0;
d.mf1(); // call Derived::mf1()
d.mf1(x); // error C2660: 'mf1' : function does not take 1 parameters(被子类遮掩)
d.mf2(); // call Base::mf2()
d.mf3(); // call Derived::mf3()
d.mf3(x); // error C2660: 'mf3' : function does not take 1 parameters(被子类遮掩)
return 0;
}
错误显示:
--------------------Configuration: tesdt - Win32 Debug--------------------
Compiling...
Text1.cpp
H:\VC6_green\MyProjects\tesdt\Text1.cpp(29) : error C2660: 'mf1' : function does not take 1 parameters
H:\VC6_green\MyProjects\tesdt\Text1.cpp(32) : error C2660: 'mf3' : function does not take 1 parameters
执行 cl.exe 时出错.
tesdt.exe - 1 error(s), 0 warning(s)
Derived class重载了Base class的部分类,以作用域为基础的“名称遮掩规则”并没有改变,base class内所有名为 mf1 和 mf3 的函数都被Derived class 内的 mf1 函数和 mf3 函数 遮掩了。从名称查找观点来看,Base::mf1 和 Base::mf3 不再被Derived继承。
如果你只想重新定义或者覆写其中一部分,那么你必须为那些原本会被遮掩的每个名称引入一个using声明式。
#include <iostream>
using namespace std;
class Base
{
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int ) { cout << "call Base::mf1(int )" << endl; }
virtual void mf2() { cout << "call Base::mf2()" << endl; }
void mf3() { cout << "call Base::mf3()" << endl; }
void mf3(double ) { cout << "call Base::mf3(double )" << endl; }
};
class Derived: public Base
{
public:
/****************************新添加的using声明式*************************/
using Base::mf1;
using Base::mf3;
//
virtual void mf1() { cout << "call Derived::mf1()" << endl; }
void mf3() { cout << "call Derived::mf3()" << endl; }
void mf4() { cout << "call Derived::mf4()" << endl; }
};
int main(void)
{
Derived d;
int x = 0;
d.mf1(); // call Derived::mf1()
d.mf1(x); // call Base::mf1(int )
d.mf2(); // call Base::mf2()
d.mf3(); // call Derived::mf3()
d.mf3(x); // call Base::mf3(double )
return 0;
}
运行结果:
call Derived::mf1()
call Base::mf1(int )
call Base::mf2()
call Derived::mf3()
call Base::mf3(double )
Press any key to continue
如果你并不想继承Base class的所有函数,那么可以选择采用private。假设Derived 以privae 形式继承Base,而Derived唯一想要继承的mf1是那个无需参数版本。using 声明式在此派不上用场,因为using声明式会令继承而来的某给定名称之所有同名函数在Derived class中可见。所以我们得采用一个简单的转交函数(forwarding function):
原书是:
#include <iostream>
using namespace std;
class Base
{
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int ) { cout << "call Base::mf1(int )" << endl; }
};
class Derived: private Base
{
public:
virtual void mf1() // 转交函数 (forwarding function), inline函数
{
cout << "call Derived::mf1()" << endl;
Base::mf1();
}
};
int main(void)
{
Derived d;
int x = 0;
d.mf1(); // call Derived::mf1()
//d.mf1(x); // error C2660: 'mf1' : function does not take 1 parameters
return 0;
}
VC6.0编译可以通过,链接出错:
--------------------Configuration: tesdt - Win32 Debug--------------------
Linking...
Text1.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Base::mf1(void)" (?mf1@Base@@UAEXXZ)
Debug/tesdt.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
tesdt.exe - 1 error(s), 0 warning(s)
我想了一下,总感觉怪怪的,因为基类Base中纯虚函数:virtual void mf1() = 0 在派生类Derived中并没有实现,所以直接调用一个纯虚函数,肯定不会呀!!
尝试修改-将基类的纯虚函数去除,改成 非纯 虚函数:
#include <iostream>
using namespace std;
class Base
{
private:
int x;
public:
/**********修改处***************/
virtual void mf1() {cout << "call Base::mf1()" << endl; };
//virtual void mf1() = 0;
virtual void mf1(int ) { cout << "call Base::mf1(int )" << endl; }
};
class Derived: private Base
{
public:
virtual void mf1() // 转交函数 (forwarding function), inline函数
{
cout << "call Derived::mf1()" << endl;
Base::mf1();
}
};
int main(void)
{
Derived d;
int x = 0;
d.mf1(); // call Derived::mf1()
//d.mf1(x); // error C2660: 'mf1' : function does not take 1 parameters
return 0;
}
运行结果:
call Derived::mf1()
call Base::mf1()
Press any key to continue
如果子类中mf1仍然是纯虚函数,那派生类所谓的转交函数该怎么写?还是说这个转交函数的前提,就是子类那个不能是纯虚函数?小弟很是困惑。有大神知道的话,希望能够留言解答一下,万分感谢。
并且还有一个疑问:
为什么可以调用子类的private成员函数?难道动态联编,不会检测权限?
后来看了第34条款,豁然开朗:
在Base class里对纯虚函数mf1增加一个默认(缺省)实现就可以了:
class Base
{
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int ) { cout << "call Base::mf1(int )" << endl; }
};
// 增加一个默认实现
void Base::mf1() {cout << "call Base::mf1()" << endl; };
class Derived: private Base
{
public:
using Base::mf1;
virtual void mf1() // 转交函数 (forwarding function), inline函数
{
cout << "call Derived::mf1()" << endl;
Base::mf1();
}
};
int main(void)
{
Derived d;
int x = 0;
d.mf1(); // call Derived::mf1()
//d.mf1(x); // error C2660: 'mf1' : function does not take 1 parameters
return 0;
}
运行结果:
call Derived::mf1()
call Base::mf1()
Press any key to continue
我想原书代码应该是这个意思。大家了可以后后续的第34条款。