⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18基类和派生类同名成员函数问题.txt

📁 里面的代码是自己写的,构思来自于c++primer,对于学习c++语法有非常大的帮助,希望对c++初学者有所帮助
💻 TXT
字号:
/*本程序为了说明:
  基类和其派生类的成员函数 的覆盖,隐藏,和 重载(用using 申明)的问题。
* 详细说明请见c++ primer 3/e P797(指象派生类的 基类指针或引用(如Base *pb = new derived中的pb)的访问范围) ,和
* P806页的继承下的类域
*/
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class A {
public:
	void  Adosth( string a );                             //本程序的关键函数,其派生类中也有
};

void A::Adosth( string a )
{
	cout << "A::Adosth() is called" << endl;
}


class B: public A {
public:
	//using A::Adosth;
	void Adosth( int a );                                 //派生类中的同名函数
private:
    
};

void B::Adosth( int a )
{
	cout << "B::Adosth() is called!" << endl;
}


void main()
{
	string haha;
	B bb;
   //下句:如果类A中的函数申明是 void  Adosth(string a);在B中是 void Adosth(int a);则 error C2664: “B::Adosth” : 不能将参数 1 从“std::string”转换为“int”
   //但是如果类B中加入using A::Adosth;,则形成重载,编译和运行都正确,且调用的是A中的Adosth;
    bb.Adosth(2);   
 

	
	A *pa = &bb;
	//下句:如果A中是void  Adosth(string a);B中void Adosth(int a); 或是using A::Adosth;void Adosth(int a);都会出现编译出错
	// error C2664: “A::Adosth” : 不能将参数 1 从“int”转换为“std::string”
	//pa->Adosth(2);
    //而在以上的情况下此句正确
	pa->Adosth(haha);
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -