c++ tricks
1 关于virtual关键字的实验
1.1 在派生类中改变virtual函数访问权限
定义两个类A,B,其中B公有派生于A。A中定义一个private成员虚函数func,B中覆写此函数,但是将其访问权限设置为public
class A{
private:
virtual void func(){
printf("A\n");
}
};
class B
: public A{
public:
void func(){
printf("B\n");
}
void do_func(){
func();
}
};
int main() {
/* test-1
* 编译报错,error: ‘virtual void A::func()’ is private
* 分析:基类中virtual修饰函数的访问权限在派生类中最好不要改变,否则发挥多态性受到限制,对比 test-1 和 test-2
**/
A * b = new B;
b->func();
/* test-2
* 编译,运行成功,
* 输出:B
**/
B * b = new B;
b->func();
/* test-3
* 编译,运行成功,
* 输出:B
**/
A * b = new B;
b->do_func();
return ;
}
2 枚举类
现在有这样一种要求,构造的对象都基于既定的模板,不允许任意构造。比如新建一个人姓氏的类,但是姓氏是固定的,不允许随便构造新的姓氏,于是可以定义枚举类。将除了Family_Name(const char * name)以外的构造函数(拷贝,赋值)设为public。然后定义若干static Family_Name供用户使用。
注: 枚举类不能为抽象类
class Family_Name{
private:
Family_Name(const char * name)
:name_(name){}
const char * name_;
public:
Family_Name(const Family_Name & other){
name_=other.name_;
}
Family_Name & operator=(const Family_Name & other){
name_=other.name_;
return *this;
}
public:
static Family_Name yang;
static Family_Name zhang;
static Family_Name liu;
static Family_Name zhao;
};
Family_Name Family_Name::yang("yang");
Family_Name Family_Name::zhang("zhang");
Family_Name Family_Name::liu("liu");
Family_Name Family_Name::zhao("zhao");
int main() {
Family_Name a = Family_Name::yang;
return ;
}
c++ tricks的更多相关文章
- testng 教程之使用参数的一些tricks配合使用reportng
前两次的总结:testng annotation生命周期 http://www.cnblogs.com/tobecrazy/p/4579414.html testng.xml的使用和基本配置http: ...
- (转) How to Train a GAN? Tips and tricks to make GANs work
How to Train a GAN? Tips and tricks to make GANs work 转自:https://github.com/soumith/ganhacks While r ...
- Matlab tips and tricks
matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...
- LoadRunner AJAX TruClient协议Tips and Tricks
LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...
- 【翻译】C# Tips & Tricks: Weak References - When and How to Use Them
原文:C# Tips & Tricks: Weak References - When and How to Use Them Sometimes you have an object whi ...
- 神经网络训练中的Tricks之高效BP(反向传播算法)
神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...
- Hex-Rays Decompiler Tips and tricks Volatile memory
https://www.hex-rays.com/products/decompiler/manual/tricks.shtml First of all, read the troubleshoot ...
- hdu 5276 YJC tricks time 数学
YJC tricks time Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- 10 Interesting Linux Command Line Tricks and Tips Worth Knowing
I passionately enjoy working with commands as they offer more control over a Linux system than GUIs( ...
- Git tricks: Unstaging files
NOTE: Following content is directly reprinted from http://andrewberls.com/blog/post/git-tricks-unsta ...
随机推荐
- linux read 系统调用剖析
https://www.ibm.com/developerworks/cn/linux/l-cn-read/ MT注:原文图1与Understanding the Linux Kernel, 3rd ...
- Log4j发送邮件
源码地址:https://github.com/mistbor/Log4jSendMail 引入activation.jar log4j.jar mail.j ...
- 关于 百度 Ueditor (在chrome浏览器) 上传图片时 打开文件夹的延迟问题
在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...
- WebLogic 任意文件上传 远程代码执行漏洞 (CVE-2018-2894)------->>>任意文件上传检测POC
前言: Oracle官方发布了7月份的关键补丁更新CPU(Critical Patch Update),其中针对可造成远程代码执行的高危漏洞 CVE-2018-2894 进行修复: http://ww ...
- multiprocess模块---进程---进程队列
首先明白几个概念: 同步:做完一件事情,再做另外一件事情 异步:做一件事情的时候,可以再做另外一件事情 阻塞:recv sleep accept input recvfrom 非阻塞:没有遇见上面这 ...
- 数据库,mysql
数据库(`database`): ### 关系型数据库及非关系型数据库1. 什么是关系型数据库? 关系型数据库是一种建立在关系模型上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据.现实世 ...
- cplexJava源码---计算结果
public static class CplexStatus implements Serializable { static final long serialVersionUID = -7367 ...
- OpenACC 书上的范例代码(Jacobi 迭代),part 3
▶ 使用Jacobi 迭代求泊松方程的数值解 ● 使用 data 构件,强行要求 u0 仅拷入和拷出 GPU 各一次,u1 仅拷入GPU 一次 #include <stdio.h> #in ...
- H5特性回顾
canvas 绘画, video 媒介回放 audio元素 对本地离线存储的更好支持, 新的特殊内容 - 元素 比如 article,footer,header,nav,section, 新的表单控件 ...
- urllib 和urllib2 模块使用简例
一.最简单的使用 import urllib,urllib2 response = urllib2.urlopen("https://www.baidu.com") print r ...