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. SQL中去掉字符串中最后一个字符(小技巧)

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

  2. 单例模式(Singleton)

    单例模式  Singletonn Pattern Ensure a class has only one instance, and provide  a global point of access ...

  3. Java并发编程学习路线(转)

    以前特地学过并发编程,但是没怎么学进去,不太喜欢.最近发现,作为一个资深工程师,却没有完整深入系统的学习过,而反是现在的BAT大并发是必须的,感觉甚是惭愧. 故找了一片学习文章,如下,准备集中一段时间 ...

  4. c++官方文档-枚举-联合体-结构体-typedef-using

    #include<iostream> #include <new> #include<stdio.h> using namespace std; /** * url ...

  5. OpenACC 优化矩阵乘法

    ▶ 按书上的步骤使用不同的导语优化矩阵乘法 ● 所有的代码 #include <iostream> #include <cstdlib> #include <chrono ...

  6. 31. Studio获取新的ID值方法

    var fun = ABS_LOADBEAN("com.plug.FunctionHelper");var vid1 = fun.utilHelper.getNextID(&quo ...

  7. xe Style

    //注意引用:vcl.themes, vcl.styles, IOutils procedure TForm1.FormCreate(Sender: TObject); var stylename: ...

  8. VBA 检查模块中是否有某个函数

    Function FindProcedures(ByRef wb As Workbook, ByVal Proc As String) As Boolean    On Error GoTo Exit ...

  9. javaEncode

    1.MD5加密 md5多用于用户密码加密或者签名使用,因md5不可逆,可用于身份验证. MessageDigest md5=MessageDigest.getInstance("MD5&qu ...

  10. webserive学习记录4-获取天气的例子

    学习到了如何创建使用webservice服务,下面就实际应用一下,从网络上获取天气数据. 先从网络上找到免费的webservice服务, 如这个网站:http://www.webxml.com.cn/ ...