- 浏览: 399951 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1012RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 772It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 963As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 773There are several built-in type ... -
c++ - virtual inheritance
2012-10-12 08:58 978As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 975As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1330in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 986template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 855the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 588The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 874A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1188You can overload the subscript ...
相关推荐
13.2 Polymorphism and virtual functions 13.3 Using inheritance to solve our problem 13.4 A simple handle class 13.5 Using the handle class 13.6 Subtleties 13.7 Details Chapter 14 Managing memory ...
#### Chapter 1: Compiling and Running a C++ Program In this chapter, the focus is on understanding the process of creating a C++ program from start to finish. The key points covered include: - **...
### Chapter 1: Writing an ANSI C++ Program This chapter introduces the reader to the fundamentals of writing a C++ program. It covers: - **Getting Ready to Program:** Discusses the setup required to ...
for example, virtual and recursive functions are not normally inlined. Usually recursive functions should not be inline. The main reason for making a virtual function inline is to place its ...
在C++中,"ex"通常代表example,而"11b"可能表示第11章的第二个示例,异常处理在C++中是一个重要的错误处理机制,确保程序在遇到错误时能够优雅地运行。 2. **ex06a**:可能是关于类和对象(Classes and Objects)...
#### 1. Introducing C# and .NET **Object Orientation** C# is an object-oriented programming (OOP) language, which means that it structures code around objects rather than actions and data instead of ...
Part I: The C# Language 1 Chapter 1: .NET Architecture 3 The Relationship of C# to .NET 4 The Common Language Runtime 4 Advantages of Managed Code 4 A Closer Look at Intermediate Language 7 Support ...
3.9.2. Interacting with C++....................................................................................45 PART 2—CORE CONSTRUCTS AND TECHNIQUES OF THE LANGUAGE Chapter 4—Basic constructs ...
#### 1. 抽象类 (Abstract Class) 抽象类是一种不能被实例化的类,它主要用于提供一个基类供其他类继承。抽象类可以包含抽象方法(没有实现的方法体)和其他非抽象方法。通过定义抽象类,可以为一组相似的对象提供一...
1 Overview ...................................................................................................... 3 1.1 Introduction ......................................................................
3.9.2. Interacting with C++....................................................................................45 PART 2—CORE CONSTRUCTS AND TECHNIQUES OF THE LANGUAGE Chapter 4—Basic constructs ...