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. 基于Linux的Samba开源共享解决方案测试(五)

    对于客户端的网络监控如图: 双NAS网关50Mb码率视音频文件的稳定写测试结果如下: 100Mb/s负载性能记录 NAS网关资源占用 稳定写 稳定写 CPU空闲 内存空闲 网卡占用 NAS1 16个稳 ...

  2. SQL中去掉字符串中最后一个字符(小技巧)

    --长度减一就可以了 select left(字段名,len(字段名)-1) from 表名

  3. Python之函数——基础篇

    函数 函数,在BASIC中,叫subroutine(子过程或子程序),在Pascal中叫做procedure(过程)和function,在C中只有function,在Java中叫method. 定义: ...

  4. Python之——遇到的小知识点总结

    学习过程中,难免会遇到一些冷门的小知识点,熟悉这些小知识可以在工作中达到事半功倍的效果,尽力消除自己的知识盲区.总之当时的自己花了不少功夫去解决这些问题,因此觉得有必要单独记录下来,以后也许会再遇到, ...

  5. 第一个python抓取单网页的例子

    #!/usr/bin/env python # coding=utf-8 import requests from bs4 import BeautifulSoup import pymysql im ...

  6. Linux---CentOS 定时运行脚本配置练手

    1.安装crontab yum install vixie-cron yum install crontabs vixie-cron软件包是cron的主程序: crontabs软件包是用来安装.卸装. ...

  7. C# ADO.NET 封装的增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. Delphi Berlin 窗体代码分离风格 回到Delphi7传统风格

    回到Delphi7传统风格 Floating Form Designer You can now detach the form designer in both, VCL and FireMonke ...

  9. Win7 系统还原

    Win7 由于某种原因,第二天开机不正常,桌面配置丢失,桌面上的文档不见了. 这种情况不要怕. 可以在启动界面F8,进入系统还原,然后选择某个时间点还原成功!!! 错误描述: Windows 不能加载 ...

  10. Find 和 Findstr

    FIND 在文件中搜索文字字符串. FINDSTR 在文件中搜索字符串.   findstr能用正则表达式,而find不能   dir c:|find /N /I /C "windows&q ...