c++模版的一个好玩的特性
2013-01-16 16:32
295 查看
这个代码展示了private继承和模版的一个妙用:
#include <iostream> #include <memory> template<typename T> class Counter { public: Counter() { count++; } Counter(const Counter&) { count++; } ~Counter() { --count; } public: std::size_t howMany(void) { return count; } private: static std::size_t count; }; template<typename T> std::size_t Counter<T>::count = 0; class B : private Counter<B> { public: B() { } ~B() { } using Counter<B>::howMany; private: int val; }; int main(void) { std::auto_ptr<int> pInt(new int(5)); std::cout << *pInt << std::endl; B b1; { B b2; std::cout << b1.howMany() << " " << b2.howMany() << std::endl; } std::cout << b1.howMany() << std::endl; return 0; }
相关文章推荐
- 用C++的高级模版特性实现一个不需要IDL的RPC
- 用C++的高级模版特性实现一个不需要IDL的RPC
- C++ const&的一个特性
- sol-v2.0 一个具有高级特性和出众性能的 C++&Lua 绑定库
- c++ 模版函数的定义和实现必须放在一个文件中
- 分享一个c++ 加密算法 ,在百度贴吧找的,比较好玩
- c++和java语言特性的不同(一个c++开发者眼中的java)
- C++中sizeof的一个特性
- 一个实例介绍C++复制控制(有一个C++11新特性要注意)——复制构造函数、赋值操作符、析构函数
- C/C++ 结构体的一个高级特性 ―― 指定成员的位数
- C/C++ 结构体的一个高级特性 ―― 指定成员的位数
- “多态性”是C++最关键和核心的一个特性,“动态绑定技术”是C++编译器最重要的一个技术!欢迎C++高手进来指正我的粗浅理解
- C++果然还是有一个模版写起来要舒服点
- cocos2d-x C++ 利用C++11新特性+runAction RepeatForever Sequence DelayTime做一个持续循环函数 每秒更新 5次后停止
- 【代码备忘】今天突然写的一个比较奇葩的C++ 代码,模版,friend,友元...
- C++ 中不能被继承的类实现,及从中体现virtual 继承的一个特性
- C++的优秀特性5:模版
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式
- c++ 模版函数的定义和实现放在一个文件中
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式