- 浏览: 400855 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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)
最新评论
As we have discussed in the post - c++ type of inheritance, we have covered private inheritance (a.k.a ) implementation inheritance, protected inheritance and public inheritance (a.k.a type inheritance). In this topic we are going to discuss virutal inheritance.
However, called virtual inheritance, it does not have close relationship with the private/protected/public inheritance. virtual inheritance is often used in the cases where multiple inheritance hierarichy is used.
For our discussion, let's first see an class hierarchy.
hierarchy_example 1 ZooAnimal Endangered / \ / -V- -V- / / \ / Bear Racoon / \ \ / ------------\ \ / \ \ --------- / \ \ / ------ Panda
ZooAnimal is a virtual base class of Raccon and Bear.
Before we dive into the virtual inheritance, let's first examine the problem without virtual base/inheritance.
C++ is a language which supports multiple inheritance. We have learned that when a class subclass another class, the super class become a subobject of the derived class, suppose if through multiple inheritance tree, a class appears twice on both side, then it has some problem this has two implications
1. it waste spaces
2. a more serious problem is ambiguity, which member method/data object to call?
virtual inhertiance is just to address the issue we have identified earlier. under virtual inheritance: there is only a single, shared base class subobject is inherited regardless of how many times the base class occurs within the derivation hierarchy.
so the importance of virtual inheritance under multiple inhertiance is said, let see some code examples.
/* * ZooAnimal to support a void paramter constructor */ class ZooAnimal { public: ZooAnimal(string name, bool onExhibit, string fam_name) : _name(name), _onExhibit(onExhibit), _fam_name(fam_name) {} // We will see under what situation we need a void parameter ctor // ZooAnimal() : _name(NULL), _onExhibit(false), _fam_name(NULL) {} virtual ~ZooAnimal(); virtual ostream& print (ostream &) const; string name() const { return _name; } string family_name() const { return _fam_name; } private: protected: bool _onExhibit; string _name; string _fam_name; } ;
class Bear: public virtual ZooAnimal { public: enum DanceType { two_left_feet, macarena, fandango, waltz } ; Bear(string name, bool onExhibit= true) : ZooAnimal(name, onExhibit, "Bear") , _dance(two_left_feet) {} virtual ostream & print(ostream& ) const; void dance(DanceType ); private: protected: DanceType _dance; };
class Raccoon : virtual public ZooAnimal { public: Raccoon(string name, bool onExhibit = true) : ZooAnimal(name, onExhibit, "Raccoon"), _pettable(false) {} virtual ostream& print(ostream& ) const; bool pettable() const { return _pettable; } void pettable(bool petval) { _pettable = petval; } private: protected: bool _pettable; };
class Endangered { public: enum CriticalLevel { critical, high, low, trivial }; enum EndangeredCause { environment, population } ; Endangered(EndangeredCause cause, CriticalLevel level) : _cause(cause), _level(level) { } private: protected: CriticalLevel _level; EndangeredCause _cause; };
class Panda : public Bear, public Raccoon, public Endangered { public: Panda(string name, bool onExhibit = true) ; virtual ostream& print(ostream &) const; bool sleeping () const { return _sleeping; } private: protected: bool _sleeping; };
Special Initialization Semantics
Panda::Panda(string name, bool onExhibit) : ZooAnimal (name, onExhibit, "Panda"), Bear(name, onExhibit), Raccoon(name, onExhibit), Endangered(Endangered::environment, Endangered::critical), _sleeping(false) { }
class Bear: public virtual ZooAnimal { public: Bear(string name, bool onExhibit= true) : ZooAnimal(name, onExhibit, "Bear") , _dance(two_left_feet) {} protected: // when an intermediate derived class Bear() : _dance( two_left_feet) {} // rest are the same.. };
class ZooAnimal { protected: ZooAnimal() : _name(NULL), _onExhibit(false), _fam_name(NULL) {} } ;
Panda::Panda(string name, bool onExhibit) : ZooAnimal (name, onExhibit, "Panda"), Endangered(Endangered::environment, Endangered::critical), _sleeping(false) { }
Construcotr and destructor order
/** * hierarchy_example 2 * * Character ZooAnimal ToyAnimal * ^ ^ ^ * | v v * | | / * RockCharacter Bear / * ^ ^ / * | | / * \ / --- - * \ / / * TdeddyBear * */ /* * there is a basic rule in terms the order of the Constructor and Destructor Order * Virtual base classes are always constructed prior to to nonvirutal base classes regardless where they appear in the inheritance hierarchy. * Let's see the example above, we are going to illustrate the discussion we have seen before. * */ class Character {} ; class BookCharacter : public Character { }; class ToyAnimal { } class TeddyBear : public BookCharacter, public Bear, public virtual ToyAnimal { };
TeddyBear Paggington;
ZooAnimal(); ToyAnimal(); Character() BookCharacter() Bear(); TeddyBear()
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15345首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1019RPath 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 971As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1064typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 779There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 829we have discussed the virtual i ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 961Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 980As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1000As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1334in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 943class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 991template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1213There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 860the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 593The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1173In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 877A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1193You can overload the subscript ...
相关推荐
C++通过虚函数(Virtual Functions)和纯虚函数(Pure Virtual Functions)实现多态,使得基类指针或引用可以调用派生类的重写方法。 封装是将数据和操作数据的方法绑定在一起,对外部隐藏内部实现细节。C++通过...
面向对象编程主要包括类(class)、对象(object)、继承(inheritance)、多态(polymorphism)等概念。 #### 1. 类与对象 - **类**:是一种用户自定义的数据类型,它可以包含数据成员和成员函数。 - **对象**:是类的一...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...
在C++中,使用虚继承(virtual inheritance)可以解决这个问题。 在给定的代码示例中,`Derived` 类从 `BaseA` 和 `BaseB` 多重继承,导致了指针 `pa` 和 `pb` 的地址不同。这是因为在内存布局中,`Derived` 对象...
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 ...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...
"inheritance---derived-class.rar_inheritance"这个压缩包文件显然包含了关于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...
virtual void f() { cout ; } }; class Son : public Base { public: void f() { cout ; } }; int _tmain(int argc, _TCHAR* argv[]) { Base b; Son s; int size_b = sizeof(b); int size_s = sizeof(s); ...
### 知识点一:C++中的reinterpret_cast `reinterpret_cast` 是C++中的一种类型转换操作符,它允许程序员将一种类型的指针重新解释为另一种类型的指针。这种转换在底层上通常涉及到对指针值的直接修改,并不涉及...
二、虚继承(Virtual Inheritance) 虚继承在派生类继承多个有共同基类时特别有用,可以避免“菱形问题”(即多重继承时可能出现的二义性)。以下是一个示例: ```cpp class Person { public: Person() { cout ...
- 继承(inheritance),可能创建一个基类GameObject,然后让Snake和Food继承它,共享公共属性和行为。 - 封装(encapsulation),通过设置访问修饰符保护对象的内部状态,只允许通过特定接口进行修改。 - 多态...
- 学习 C++ 的核心概念,如类(class)、对象(object)、继承(inheritance)等。 - 理解 C++ 中的关键特性,包括友元(friend)、虚函数(virtual)/运行时类型识别(RTTI)、const 和 mutable 关键字、异常(exception)处理...
C++通过虚函数(virtual functions)和纯虚函数(pure virtual functions)实现多态性,使得基类指针或引用可以调用派生类的重写方法。 除了以上核心概念,Visual C++环境还提供了一些额外的工具和特性,如MFC...
C++默认采用二义性解析,可以通过虚继承(virtual inheritance)来解决。 5. **虚继承(Virtual Inheritance)** 虚继承通过在基类前加上`virtual`关键字来实现,可以消除钻石问题。这样,派生类只有一个共享的...
在C++中,可以通过虚函数(Virtual Function)实现运行时多态。 4. **封装**:封装是将数据成员和成员函数组合在一个单独的单元——类中,并控制外部对它们的访问程度,以保护数据的完整性和安全性。 #### C++语言...