网上的一般结论是:
在C++中,对于两个函数,一个有const修饰,一个没有const修饰,认为这两个函数是不同的函数。
虚函数的要求是,函数原型相同,函数原型包括:函数返回值、函数名、参数列表、const修饰符。这里const修饰符包括函数返回值的修饰,函数形参的修饰,函数本身的修饰。只要有一处没有对上 ,那么就不是虚函数的override,而是调用基类的同名函数。所以对于基类的cosnt虚函数,如果子类重写忘记加上const,编译器会认为是基类的函数。
只需要记住以下结论就行
在子类写继承自父类的虚函数,若该在父类中虚函数末尾写了const,而在子类中没有写,那么将认为子类中写的函数不是父类函数的重写/覆盖;同理,若该虚函数在父类中没有const,而在子类中加上了const则认为子类中写的函数也不是父类函数的重写/覆盖。
关注以下几个例子:
1. 虚函数末尾的const
a. 父类、子类虚函数末尾均有const
#include<iostream>
using namespace std;
class Base{
protected:
int x;
public:
Base(int a=0):x(a){};
virtual void display() const {cout << x << " //调用基类函数" << endl;}
// 这里有const
};
class Derived: public Base{
protected:
int y;
public:
Derived(int b=0):y(b){};
void display() const {cout << x << "," << y << " //调用子类函数" << endl;}
// 这里有const
};
int main()
{
Base b(1);
Derived d(2);
Base* p = &d;
b.display();
d.display();
p->display();
return 0;
}
运行结果:
b. 父类有,子类无
#include<iostream>
using namespace std;
class Base{
protected:
int x;
public:
Base(int a=0):x(a){};
virtual void display() const {cout << x << " //调用基类函数" << endl;}
// 这里有const
};
class Derived: public Base{
protected:
int y;
public:
Derived(int b=0):y(b){};
void display() {cout << x << "," << y << " //调用子类函数" << endl;}
// 这里没有const
};
int main()
{
Base b(1);
Derived d(2);
Base* p = &d;
b.display();
d.display();
p->display();
return 0;
}
运行结果:
c. 父类无,子类有
#include<iostream>
using namespace std;
class Base{
protected:
int x;
public:
Base(int a=0):x(a){};
virtual void display() {cout << x << " //调用基类函数" << endl;}
// 这里没有const
};
class Derived: public Base{
protected:
int y;
public:
Derived(int b=0):y(b){};
void display() const {cout << x << "," << y << " //调用子类函数" << endl;}
// 这里有const
};
int main()
{
Base b(1);
Derived d(2);
Base* p = &d;
b.display();
d.display();
p->display();
return 0;
}
运行结果
2. 虚函数参数列表中的const
a. 均有const:
#include<iostream>
using namespace std;
class Base{
protected:
int x;
public:
Base(int a=0):x(a){};
virtual void display(const int a){cout << x << " //调用基类函数" << endl;}
};
class Derived: public Base{
protected:
int y;
public:
Derived(int b=0):y(b){};
void display(const int a){cout << x << "," << y << " //调用子类函数" << endl;}
};
int main()
{
Base b(1);
Derived d(2);
Base* p = &d;
b.display(0);
d.display(0);
p->display(0);
return 0;
}
b. 父类有,基类无(或反之),似乎无影响。。
#include<iostream>
using namespace std;
class Base{
protected:
int x;
public:
Base(int a=0):x(a){};
virtual void display(const int a){cout << x << " //调用基类函数" << endl;}
};
class Derived: public Base{
protected:
int y;
public:
Derived(int b=0):y(b){};
void display(int a){cout << x << "," << y << " //调用子类函数" << endl;}
};
int main()
{
Base b(1);
Derived d(2);
Base* p = &d;
b.display(0);
d.display(0);
p->display(0);
return 0;
}
3. 函数返回值的const
a. 均有:
#include<iostream>
using namespace std;
class Base{
protected:
int x;
public:
Base(int a=0):x(a){};
virtual const void display(){cout << x << " //调用基类函数" << endl;}
};
class Derived: public Base{
protected:
int y;
public:
Derived(int b=0):y(b){};
const void display(){cout << x << "," << y << " //调用子类函数" << endl;}
};
int main()
{
Base b(1);
Derived d(2);
Base* p = &d;
b.display();
d.display();
p->display();
return 0;
}
b. 其一无,则编译错误。