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 ...
随机推荐
- css模拟下拉菜单
<!DOCTYPE html > <head> <meta http-equiv="Content-Type" content="t ...
- nodejs中https请求失败,无报错
今天群里一位同学在做练习的时候,采用https例子: // curl -k https://localhost:8000/ const https = require('https'); const ...
- 温故而知新-mysql高级管理
温故而知新-mysql高级管理 1 mysql的一些授权信息都保存在授权表中 授权表是6个 db,user,host,tables_priv,columns_priv,procs_priv 这6个表 ...
- HTML5 Canvas ( 矩形的绘制 ) rect, strokeRect, fillRect
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 15.Result配置详解
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 说明:在前面的许多案例中我们所用到的Action基本都继承自ActionSu ...
- mysql 修改用户密码
修改mysql用户密码 目录 mysqladmin命令 UPDATE user 语句 SET PASSWORD 语句 root密码丢失的情况(待验证) mysqladmin命令(回目录) 格式如下 ...
- jquery.ajax的url中传递中文乱码问题的解决方法
jquery.ajax的url中传递中文乱码问题的解决方法 JQuery JQuery默认的contentType:application/x-www-form-urlencoded 这才是JQu ...
- Jsonlib 属性过滤器
/** * @title JSON转换属性过滤器 * @description 用于JSON lib的JSON转换 * @author maohuidong * @date 2017-04-06 */ ...
- Delphi中拖动的方式来移动TPageControl的Tab
procedure TMainForm.PageControl1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftStat ...
- DELPHI WM_CopyData 用法
unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...