const修饰虚函数
【1】程序1
#include <iostream>
using namespace std; class Base
{
public:
virtual void print() const = ;
}; class Test : public Base
{
public:
void print();
}; void Test::print()
{
cout << "Test::print()" << endl;
} void main()
{
// Base* pChild = new Test(); //compile error!
// pChild->print();
}
【2】程序2
#include <iostream>
using namespace std; class Base
{
public:
virtual void print() const = ;
}; class Test : public Base
{
public:
void print();
void print() const;
}; void Test::print()
{
cout << "Test::print()" << endl;
} void Test::print() const
{
cout << "Test::print() const" << endl;
} void main()
{
Base* pChild = new Test();
pChild->print();
}
/*
Test::print() const
*/
【3】程序3
#include <iostream>
using namespace std; class Base
{
public:
virtual void print() const = ;
}; class Test : public Base
{
public:
void print();
void print() const;
}; void Test::print()
{
cout << "Test::print()" << endl;
} void Test::print() const
{
cout << "Test::print() const" << endl;
} void main()
{
Base* pChild = new Test();
pChild->print(); const Test obj;
obj.print(); Test obj1;
obj1.print(); Test* pOwn = new Test();
pOwn->print();
} /*
Test::print() const
Test::print() const
Test::print()
Test::print()
*/
备注:一切皆在代码中。
总结:const修饰成员函数,也属于函数重载的一种范畴。
Good Good Study, Day Day Up.
顺序 选择 循环 总结
const修饰虚函数的更多相关文章
- const 修饰成员函数 前后用法(effective c++ 03)
目录 const在函数后面 const修饰成员函数的两个作用 const在函数前面 总结 const在函数后面 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态 ...
- C++基础-4-封装(构造函数与析构函数,深拷贝与浅拷贝,静态成员,this,友元,const修饰成员函数)
4. 封装 4.1.1 封装的意义 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 con ...
- 11. const 修饰成员函数
const 限定只读,对函数的实参进行保护 常数据成员:必须出现在类的定义体中,常数据成员必须进行初始化,并且不能被更新,但常数据成员的初始化只能通过构造函数的初始化列表进行 1. 常函数 成员函数加 ...
- inline修饰虚函数的问题
虚函数是否可以内联? 一般来说,inline是编译时的行为,虚函数是在程序执行时的行为,因此编译器一般会拒绝对虚函数进行内联!
- const 修饰函数
At the very first ,I got a problem . Vector Vector::operator+(const Vector &v)const{ return Vect ...
- C++ const修饰函数、函数参数、函数返回值
const修饰函数 在类中将成员函数修饰为const表明在该函数体内,不能修改对象的数据成员而且不能调用非const函数.为什么不能调用非const函数?因为非const函数可能修改数据成员,cons ...
- const修饰符与函数
一.用const修饰函数的参数 函数参数类型前加const指明该参数为常量,在函数内部不可改变. void func(const int x) { //x不可以在内部进行赋值等操作. } 注:当参数为 ...
- Delphi 中 函数参数中的 const 修饰符的本质以及注意事项
来自:http://blog.csdn.net/farrellcn/article/details/9096787 ------------------------------------------ ...
- cc32b_demo-32dk2j_cpp_纯虚函数与抽象类2-txwtech
cc32b_demo-32dk2j_cpp_纯虚函数与抽象类2-txwtech //纯虚函数是用来继承用的//纯虚函数//抽象类-抽象数据类型//*任何包含一个或者多个纯虚函数的类都是抽象类//*不要 ...
随机推荐
- LogBack配置详解(一)
一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配置文 ...
- AD834+表面声波滤波器调试小结
AD834:带宽较宽,300M无压力,但是输出幅度小,当输入2VPPX2VPP的时候最大输出400mVPP.一个特别要注意的地方是输入走线尽量短,敷铜间距至少满足3倍线宽,不然隔直之后还会耦合信号进去 ...
- 读写ZIP&JAR文件
1. ZipEntry 是包括目录的,也就是目录也被当做是一个单独的Entry,在列出它下面的文件之前先列出这个directory entry. 这个在解压ZIP文件的的时候特别有用,我们要先创建这个 ...
- JQuery:JQuery语法、选择器、事件处理
JQuery语法: 通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行"操作"(actions). 一.语法:jQuery 语法是通过选取 HTM ...
- 30天,APP创业从0到1【7.11郑州站】
活动概况 时间:2015年07月11日13:30-16:30 地点:慧谷咖啡(郑州杨金路9号河南外包产业园天元W栋) 主办:APICloud.环信.河南中医药大学医药信息化实验室 网址:www.api ...
- Spring Boot 4 MyBatis
SpringBoot内使用MyBatis,可以不使用xml映射配置,通过注解方式映射. pom.xml添加依赖 <dependency> <groupId>org.mybati ...
- Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Tigase Server Clustering
首先,在服务器上启用集群 修改init.properties --cluster-mode=true 自定义端口 允许自定义,但是所有的实例都要使用相同的端口,以便通讯 --cl-comp-ports ...
- Some Skills in Visual Studio
Copy file which added as link http://mattperdeck.com/post/Copying-linked-content-files-at-each-build ...
- C++ note
主要是为了学习c++的类和对象 内容摘自 c++概述 http://see.xidian.edu.cn/cpp/biancheng/cpp/rumen_1/ 1,变量 ,C++中,我们可以在 ...