[OOD-More C ++ Idioms] Inner Class)

Source: Internet
Author: User

[OOD-More C ++ Idioms] Inner Class)
The purpose of an Inner Class is to implement multiple interfaces without having to inherit multiple classes. At the same time, it can naturally convert Up-casting ). Multiple implementations of the same interface are provided under a single abstraction. Alias motivation

The virtual function Signatures provided by the two independent libraries through different interfaces may conflict. If you need to implement these two functions at the same time, the problem may occur. Example:

Class Base1 // from the Moon {public: virtual int open (int) = 0;/* virtual */~ Base1 () {}// do not allow polymorphism destructor}; class Base2 // from Jupiter {public: virtual int open (int) = 0; /* virtual */~ Base2 () {}// do not allow polymorphism destructor}; class Derived: public Base1, public Base2 {public: virtual int open (int I) {// Wow! Where did they come from? Return 0;}/* virtual */~ Derived (){}};

The internal class is used to solve this problem.

Solution and example

In the above example, the two base classes do not need to be modified. The subclass is implemented using the following method:

# Include
  
   
Class Base1 // from the Moon {public: virtual int open (int) = 0;/* virtual */~ Base1 () {}// do not allow polymorphism destructor}; class Base2 // from Jupiter {public: virtual int open (int) = 0; /* virtual */~ Base2 () {}// destructor that do not allow polymorphism}; class Derived // note that {class base‑impl; friend class base‑impl is not inherited; // note that the statement of membership class base‑impl is: public Base1 // note that the public inheritance {public: base‑impl (Derived * p): parent _ (p) {} int open () override {return parent _-> base1_open ();} private: Derived * parent _;} base1_obj; // pay attention to the member variables. class Base2_Impl; friend class Base2_Impl; // pay attention to declaring a friend class Base2_Impl: public Base2 // public inheritance {public: Base2_Impl (Derived * p): parent _ (p) {} int open () override {return parent _-> base2_open ();} private: Derived * parent _;} base2_obj; // The member variable int base=open () {return 111;} // implement int base2_open () {return 222;} // implement public: Derived (): base1_obj (this), base2_obj (this) {} operator Base1 & () {return base1_obj;} // go to Base1 & operator Base2 & () {return base2_obj;} // go to Base2 &}; /// class Derivedint base1_open (Base1 & b1) {return b1.open ();} int base2_open (Base2 & b2) {return b2.open ();} int main (void) {Derived d; std: cout <base20.open (d) <std: endl; // Like upcasting in inheritance. std: cout <base2_open (d) <std: endl; // Like upcasting in inheritance .}
  

The attached class diagram is easy to understand:

Here, the class Derived is not a subclass, but implements different interfaces through two internal Nested classes, and then bridges back to the two implemented functions defined by myself: base‑open and base2_open. The two nested classes do not share the inheritance relationship. The two conversion operators provided by the Derived class can be used to convert Derived to any base class. The other two internal class objects do not require additional lifecycle management. their lifecycles are the same as those of the Derived object.

Known applications

:
The concept of Inner Class comes from Java. This feature is that nested classes can use private member variables and member functions of external classes in the form of friends to support stronger interaction. Generally, this internal class must be private.
Take the Http Cache of the Chromium network module as an example:

This is a simple example with no multi-inheritance. It emphasizes encapsulation and Information Hiding (HttpCache: Transaction is a private class in HttpCache) OO features. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoMiBpZD0 = "related usages"> related usages Interface Class Capability Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.