虚函数中的const

 网上的一般结论是:

在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. 其一无,则编译错误。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值