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的更多相关文章

  1. testng 教程之使用参数的一些tricks配合使用reportng

    前两次的总结:testng annotation生命周期 http://www.cnblogs.com/tobecrazy/p/4579414.html testng.xml的使用和基本配置http: ...

  2. (转) 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 ...

  3. Matlab tips and tricks

    matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...

  4. LoadRunner AJAX TruClient协议Tips and Tricks

    LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...

  5. 【翻译】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 ...

  6. 神经网络训练中的Tricks之高效BP(反向传播算法)

    神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...

  7. Hex-Rays Decompiler Tips and tricks Volatile memory

    https://www.hex-rays.com/products/decompiler/manual/tricks.shtml First of all, read the troubleshoot ...

  8. hdu 5276 YJC tricks time 数学

    YJC tricks time Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  9. 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( ...

  10. Git tricks: Unstaging files

    NOTE: Following content is directly reprinted from http://andrewberls.com/blog/post/git-tricks-unsta ...

随机推荐

  1. FIO read测试结果偏离

    工作中发现一个fio问题,测试组测试出来的数据read速度一个是17.0G/s,一个是13.2G/s.要知道我后台只有24块7.2k RPM的机械硬盘啊!怎么也不可能有这样的速度. 回家之后我模拟了实 ...

  2. 基于Linux的Samba开源共享解决方案测试(三)

    在极限写场景下,对于网关的网络监控如图: 在极限写场景下,对于网关的网络监控如图: 在极限混合读写场景下,对于网关的网络监控如图: 在极限混合读写场景下,对于客户端的网络监控如图: 双NAS网关100 ...

  3. SQL SERVER 2012/ 2014 分页,用 OFFSET,FETCH NEXT改写ROW_NUMBER的用法

    写法: 假装有个表Shop,其中有一列ShopName,取100000到100050条数据. ROW_NUMBER 的写法 SELECT * FROM ( SELECT ShopName , ROW_ ...

  4. dos批处理文件中的变量小结

    很多情况下我们需要用到一些批处理文件中的变量,方便我们执行一些操作,这里简单整理下,方便需要的朋友   批处理中的变量,我把他分为两类,分别为"系统变量"和"自定义变量& ...

  5. uva-639-枚举

    题意: 象棋里的車可以吃横竖的車,题目加了一个墙,用于阻断攻击,问4x4的棋盘最多可以放多少只車, 思路:枚举每一个点,2^16次方种情况 #include<stdio.h> #inclu ...

  6. uva-10050-模拟水题

    一个社会研究组织决定通过一组简单的参数来模拟国家政党的行为.第一个参数一个正整数h(叫做罢工参数),用于指示在对应的政党在俩个连续休假之间的平均天数.虽然这个参数太简单了,它不是最完美的参数.但是它还 ...

  7. XMind8 安装

    参考:https://blog.csdn.net/qq_35911589/article/details/81901868 https://blog.csdn.net/Zjhao666/article ...

  8. delphi 加密 XOR

    From  http://www.delphigeist.com/2009/09/text-encryption-with-xor.html Text encryption with XOR   Ev ...

  9. js 层随着滚动条上下移动

    var tips; var theTop = 10; /*这是默认高度,越大越往下*/ var old = theTop; function moveTips() { var tt = 0; if ( ...

  10. as3 object与dictionary区别

    AS3中的Dictionary类(flash.utils.Dictionary)是一个新的AS类.Dictionary类和Object唯一的区别在于:Dictionary对象可以使用非字符串作为键值对 ...