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 ...
随机推荐
- MySql 链接字符串
MySql连接字符串总结 1.本地数据库连接 <connectionStrings> <add name="ConnectionString" ...
- sklearn的estimator
estimator的工作流程 在sklearn中,估计器(estimator)是一个重要的角色,分类器和回归器都属于estimator.在估计器中有有两个重要的方法是fit和transform. fi ...
- UVA-712-满二叉树
一个策略树(S-tree)是一组变量Xn={x1,x2...xn}的表现形式,它代表一个布尔函数f:{0,1}n->{0,1},策略树每条路径从根结点开始由n+1个结点组成,策略树的每一个结点都 ...
- OpenACC 简单的直方图
▶ 简单的直方图,强调原子操作的使用 ● 代码 #include <stdio.h> #include <stdlib.h> #include <openacc.h> ...
- 39. 拼接表字段b.day
var fun = ABS_LOADBEAN("com.plug.FunctionHelper");//var v_div = fun.funHelper.strAdd(" ...
- DataSanp App与Rest, WebBroker App的区别
DataSanp App与Rest, WebBroker App的区别 datasnap server :选择这一项,我们得到的将是一个独立EXE的三层服务器应用程序(TCP及HTTP两种模式) To ...
- css 学习网址
http://www.divcss5.com/ http://www.divcss5.com/css3/ css3手册 http://www.divcss5.com/shouce/ css手册 ht ...
- UI5-文档-4.14-Custom CSS and Theme Colors
有时我们需要定义一些更细粒度的布局,这时我们可以通过向控件添加自定义样式类来使用CSS的灵活性,并根据自己的喜好对它们进行样式化. Preview The space between the butt ...
- c++ static成员
static 成员通常不能在类的定义体重初始化 有一种例外,const static成员可以在定义体内初始化,并且可以用于构造函数 将函数声明为const表示该函数不能修改其所属的对象
- drop user和drop user cascade的区别
SQL> delete user itp2;delete user itp2 *第 1 行出现错误:ORA-00903: 表名无效 SQL> drop user itp2;dr ...