- 浏览: 387647 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
we have discussed the virtual inhertiance in general in the post - C++ - virtual inheritance . In this post we are not going to discuss the theory but instead we are going to show you an example so that hopefully you can get a feel of how the virtual inheritance can be useful in real world.
Let's see we are devising libraries classes , the base classes is a Array<Type> class, and we want to add more functionality based on the base class. So we are going to add some range checking abilility and then we are going to add a sorted functions. here is the code.
/** * virtual inheritance example * in this example, we are going to discuss the topic of multiple inheritance in an example * * */ template <class Type> class Array; template <class Type> ostream& operator << (ostream&, const Array<Type>&); template <class Type> class Array { static const int ArraySize = 12; public: explicit Array(int sz = ArraySize) { init (0, sz); } Array(const Type *ar, int sz) { init(ar, sz); } Array(const Array &ia) { init(ia.ia, ia.size()); } virtual ~Array() { delete[] ia; } Array& operator= (const Array&); int size() { return _size; } virtual void grow(); virtual void print (ostream& = cout); // program technique, in order to allow for better performance, // since we are going to allow user to polymorphically //retrieve the element at a specified index, // in order not to sacrifise the performance, we are making some helper class which is called // at() which can be inlined. Type at(int ix) const { return ia[ix]; } virtual Type& operator[] (int ix) { return ia[ix]; } virtual void sort(int, int); virtual int find(Type ); virtual Type min(); virtual Type max(); protected: void swap(int , int); void init(const Type* , int); int _size; Type * ia; private: }; template <class Type> class Array_RC : public virtual Array<Type> { public: // it is wrong to write as // Array_RC(int sz = ArraySize) : Array (sz) {} any reference to the base template base class type specifier must be fully // qualitified with its formal paramters list. Array_RC(int sz = ArraySize) : Array<Type> (sz) {} Array_RC(const Array_RC& r) ; Array_RC(const Type * ar, int sz); Type& operator[] (int ix); protected: private: }; #include <assert.h> template <class Type> Array_RC<Type>::Array_RC(const Array_RC<Type> &r) : Array<Type>(r) {} template<class Type> Array_RC<Type>::Array_RC(const Type * ar, int sz) : Array<Type>(ar, sz) {} template <class Type> Type& Array_RC<Type>::operator[] (int ix) { assert(ix >= 0 && ix < Array<Type>::_size); return ia[ix]; } template <class Type> class Array_Sort : public virtual Array<Type> { public: Array_Sort(const Array_Sort<Type> &); Array_Sort(const int sz = Array<Type>::ArraySize) : Array<Type>(sz) { clear_bit(); } Array_Sort(const Type* arr, int sz) :Array<Type>(arr, sz) { sort(0, Aray<Type>::_size - 1); clear_bit(); } Type operator[] (int ix) { set_bit(); return ia[ix]; } void print(ostream& os = cout ) { check_bit(); Array<Type>::print(os); } Type min() { check_bit(); return ia[0]; } Type max() { check_bit(); return ia[Array<Type>::_size - 1]; } bool is_dirty() const { return dirty_bit; } int find(Type); void grow(); protected: void set_bit() { dirty_bit = true; } void clear_bit() { dirty_bit = true; } void check_bit() { if (dirty_bit) { sort(0, Array<Type>::_size); clear_bit(); } } private: }; template <class Type> Array_Sort<Type>::Array_Sort(const Array_Sort<Type> &as) { // note: as.check_bit() does not work // -- see explanation below if (as.is_dirty()) { sort(0, Array<Type>::_size - 1 ); } clear_bit(); } /** this is a very typical implemenation of the divide 2 search */ template <class Type> int Array_Sort<Type>::find(Type value) { int low = 0; int high = Array<Type>::_size - 1; check_bit(); while (low <= high) { int mid = (low + high ) / 2; if (val == ia[mid]) return mid; if (val < ia[mid]) high = mid - 1; else low = mid + 1; } return -1; } /** * now we are going to make a class that shall inherits both the * Array_Sort and the Array_RC * and the new class would be named as * Array_RC_S * */ template <class Type> class Array_RC_S : public Array_RC<Type>, public Array_Sort<Type> { public: Array_RC_S(int sz = Array<Type>::ArraySize) : Array<Type>(sz) { Sort(0, Array<Type>::_size - 1); clear_bit(); } Array_RC_S(const Array_RC_S<Type> & rca) : Array<Type>(rca) { sort(0, Array<Type>::_size -1 ); clear_bit(); } Array_RC_S(const Type* arr, int sz) : Array<Type> (rca) { sort(0, Array<Type>::_size - 1); clear_bit(); } Type& operator[] (int index) { set_bit(); return Array_RC<Type>::operator[] (index); } };
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15277首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 958RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 742It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 913As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1029typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 732There are several built-in type ... -
c++ - virtual inheritance
2012-10-12 08:58 905As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 715There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 925Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 916As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 946As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1282in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 895class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 952template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1175There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 789the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 546The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1126In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 832A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1117You can overload the subscript ...
相关推荐
Laravel开发-single-table-inheritance 单表继承特性
Laravel开发-laravel-single-table-inheritance 包含单表继承模型的包,具有雄辩(laravel orm)
Laravel开发-eloquent-single-table-inheritance 单表继承(Sti)作为Laravel雄辩模型的特征实现
Laravel开发-eloquent-inheritance-storage 用雄辩的ORM轻松管理继承
Laravel开发-moloquent-inheritance 为moloquent模型提供单表继承。
修复 增加了和可靠的覆盖范围更新了依赖性上一篇:v0.5.1(2015-04-09)安装# Using npmnpm install gulp-sass-inheritance --save# Using yarnyarn add gulp-sass-inheritance用法您可以将gulp-sass-inheritance与...
C++ Primer Plus, Fourth Edition presents the ANSI C++ standard beginning with a discussion of the essential elements of C++ programming: loops, expressions, functions, and classes. It walks readers ...
(Although it ships at a slender 350 pages, Accelerated C++ is packed with samples, tips, and example problems; about 10 per chapter.) After a tour of basic C++, the book looks at more advanced C++ ...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...
Hibernate继承在本硬件中,您将练习不同的继承策略。 行动项目: - Fork the project. - Add required annotations in model classes. - Implement all methods in DAOs实施以下要求: 有课程:动物,猫和狗。...
this is an example for polymorphism and inheritance feature of OOP C++. A reference for C++ starter.
PostCSS 局部作用域继承示例 一个小例子,带有的。 该项目最值得注意的是使用以下工具: 具有 和 运行示例 $ npm install $ npm start & open http://localhost:8080 执照
Whether your memory dump is in raw format, a Microsoft crash dump, hibernation file, or virtual machine snapshot, Volatility is able to work with it. We also now support Linux memory dumps in raw or ...
介绍Java的封装继承和多态和java的一些基本特性
Chapter 16 - Multiple Inheritance in C++ Chapter 17 - OO for Graphical User Interfaces, A Tour of Three Toolkits Chapter 18 - Multithreaded Object-Oriented Programming Chapter 19 - Network Programming...
inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how your understanding the underlying implementation models can help you code more efficiently and with ...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"--virtual functions and virtual inheritance. This book shows how...