Pimpl usage Analysis of compiling firewall--c++

Source: Internet
Author: User

http://blog.csdn.net/lihao21/article/details/47610309

Pimpl (pointer to implementation, Pointer to Implementation) is a common method used to decouple the "interface and implementation of a class". This technique avoids exposing private details in the header file (see 1), and is therefore an important mechanism to facilitate the complete separation of API interfaces from Implementation. But Pimpl is not a strict design pattern (it is a workaround that is subject to C + + specific limitations), and this idiom can be seen as a special case of bridging design Patterns.

Figure 1:pimpl idiom, where the public class has a private pointer to the hidden implementation class

Using the Pimpl idiom in a class has the following advantages:

    • Reduced coupling
    • Information hiding
    • Reduce compilation dependencies and increase compilation speed
    • Interface and implementation separation

In order to achieve pimpl, we first look at a common class design method.
If we are going to design a Book of books that contains the directory attributes and provides an external interface for printing the information, book is designed as Follows:

class Book{public: void print();private: std::string m_Contents;};

Book users only need to know the print () interface, You can use the book class, it seems that everything is Beautiful.
however, when one day, you find that book needs to add a title attribute, the change to the class is as Follows:

class Book{public: void print();private: std::string m_Contents; std::string m_Title;};

Although the print () interface can still be used to output the information of a book directly, the user of the books class has to recompile all the code that contains the header File.

In order to hide the implementation details of the book class and implement a true separation of the interface and implementation, the Pimpl method can be Used.
We still provide the same interface to the book class, but the book class no longer contains the original data members, and all of its operations are implemented by the Bookimpl class.

/* public.h */#ifndef PUBLIC_H_INCLUDED#define PUBLIC_H_INCLUDEDclass Book{public: Book(); ~Book(); void print();private: class BookImpl; // Book实现类的前置声明 BookImpl* pimpl;};#endif

header files in the externalpublic.h, only the external interface of the book class is included, and the actual implementation details are encapsulated in theBookimplclass. To not expose the Bookimpl class, declare it as an inline class of the book class and declare it as Private.

The header file for the Bookimpl class is as Follows.

/* private.h */ #ifndef private_h_included #define private_h_included #include "public .h " #include <iostream> class book::bookimpl {public: void print (); private: std::string m_contents; std::string m_Title;};  #endif 

private.hThere is no need to provide the user of the book class, so if you need to redesign the properties of the books class later, the outside world does not know about it, keeping the interface invariant and reducing compilation dependencies between Files.
/* book.cpp */# Include "private.h" //we need to call the Bookimpl Class's member function, / /SO to include Bookimpl's definition header file  #include "public.h" //we are implementing the book class , so to include the book Class //header file book::book () {pimpl = new Bookimpl ();} Book::~book () {delete pimpl;} void book::p rint () {pimpl->print ();} /* Bookimpl class implementation function */void book::bookimpl::p rint () {std::cout <<  "print from bookimpl" << std::endl;} 

/code>
Here's How to use the book Class's interface:
/* main.cpp */#include "public.h"int main(){    Book book;    book.print();    return 0;}

Classes such as the book class that use Pimpl are often referred to as handleclass, Bookimpl classes as implementation classes, called implementationclass.

For simple implementations, the book class omits the copy constructor and the copy assignment Function. In practical application, there are two kinds of options to solve the semantic problem of copy and assignment of Book.

(1) prohibit copying class
If you do not intend to have the user create a copy of the object, you can declare the object to be Non-replicable. You can declare a copy constructor and a copy assignment function as private so that a compilation error occurs when you copy or assign a Value.
The following code does not need to modify the Associated. CPP file by declaring the private copy constructor and copying the assignment function so that the object cannot be Copied.

/* public.h * / #ifndef public_h_included# define public_h_includedclass book{public: book (); ~book (); void Print (); private://prohibit copying class book (const Book &); const book &operator = (const Book &); Class bookimpl; //book implements the predecessor declaration of the class bookimpl* pimpl;}; #endif         

(2) Display definition replication semantics
If you want users to be able to replicate objects that take pimpl, you should declare and define your own copy constructors and copy assignment Functions. They can perform a deep copy of the object, which is to create a copy of the object, not a copy pointer.

The main disadvantage of Pimpl idioms is that each object you create must be assigned and disposed of the implementation object, which adds a pointer to the object, and each invocation of the handle class member function must pass the implementation class, which adds a layer of Indirection. In practice, you need to weigh these costs.
In addition, with the Pimpl object, the compiler will no longer be able to capture changes to member variables in the const METHOD. This is because member variables now exist in separate objects, and the compiler only checks that the Pimpl pointer in the Const method changes, and does not check any of the members that Pimpl points to.

You can use the Pimpl method to illustrate the role of the book class design in the Above:


Figure 2:pimpl as a compiled firewall

Pimpl is often referred to as a "compile-time firewall" because Pimpl relieves the coupling between interfaces and implementations, thereby reducing compilation dependencies between Files.

The sample code for this article can be downloaded via the following link: download link

Resources
      1. C + + Programming/idioms:pointer to implementation (pimpl)
      2. The C + + Pimpl
      3. Compilation firewalls
      4. "effective C + + (third edition)", Scott meyers, Houtie
      5. Pimpl mode
      6. "c + + API design", Martin reddy,

Pimpl usage Analysis of compiling firewall--c++

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.