const 函数调用类的非const成员函数报错
来源:互联网 发布:mac os x leopard下载 编辑:程序博客网 时间:2024/06/14 00:50
class Abstract{public:virtual void foo() = 0;};class Derived : public Abstract{public:virtual void foo(){std::cout << "Derived::foo()" << std::endl;}};void PrintAbstract(const Abstract &a){a.foo();}int main(){Derivedd;PrintAbstract(d);}class Abstract{public:virtual void foo() const = 0;};class Derived : public Abstract{public:virtual void foo() const{std::cout << "Derived::foo()" << std::endl;}};报错:
error: passing 'const Abstract' as 'this' argument of 'virtual void Abstract::foo()' discards qualifiers
原因:const函数PrintAbstract()调用类中的非const函数foo()
解决方法:
将foo()函数声明为const函数:
class Abstract{public:virtual void foo() const = 0;};class Derived : public Abstract{public:virtual void foo() const{std::cout << "Derived::foo()" << std::endl;}};
- const 函数调用类的非const成员函数报错
- const对象调用非const成员函数
- 类的const和非const成员函数的重载
- 类的const和非const成员函数的重载
- 类的const和非const成员函数的重载
- 类的const和非const成员函数的重载
- 类的const和非const成员函数的重载
- 类的const和非const成员函数的重载
- const成员函数和非const的成员函数之间的调用
- 为什么const对象只能调用const成员函数,而不能调用非const成员函数?
- 为什么const对象只能调用const成员函数,而不能调用非const成员函数?
- 非const对象也可以调用const成员函数
- 非const对象也可以调用const成员函数
- C++的const类成员函数(解释为什么非const成员函数不能访问const对象的数据成员)
- 类的 const 成员函数
- 类的const成员函数
- 类的const成员函数
- 类的const成员函数
- vi
- Spring依赖注入
- Android原生(Native)C(JNI/NDK)开发之一:环境搭建篇
- 解决ActiveX部件不能创建对象的有效方法
- Android原生(Native)C(JNI/NDK)开发之二:framebuffer篇
- const 函数调用类的非const成员函数报错
- poj 2081 :Recaman's Sequence
- Control的Invoke和BeginInvoke 区别
- Hessian介绍
- 目录扫描printfdir
- 低调做人,中调做事
- 在uboot中添加新的命令
- 在freescale平台下,添加一个GPIO驱动(power key)
- fastboot命令驱动修改(uboot-imx/drivers/fastboot/fastboot.c)