Handle class and Interface Class

Source: Internet
Author: User

Both are used to reduce compilation dependency between files.

1. Compilation dependency

# Include " File1.h "
# Include " File2.h "

Class Class_name
{
Member1 m_m1;
Member2 m_m2;
Public:
Member1 get_member_1 ()Const{};
Member2 get_member_2 ()Const{};
} ;

Assume that the header file is file. h. When file1.h or file2.h changes, or the implementation of class_name in file changes, all include file. H files have to be re-compiled when file. when H is contained in many files, it takes a lot of compilation time even if you make a small change to class_name.

2. Handle class (handle class)
Handle classs only improves all interfaces and contains a pointer to the real implementation class. The real implementation class is included in another file. to modify this class, only file. H will cause re-compilation, while other files containing file. H will not cause re-compilation.

# Include " File1.h "// Contain member1
# Include " File2.h "// contain member2
# Include "Implement. H"

Class Class_name
{
Class_impl * Implement; // Generally, shared pointer is used.

Public :
Member1 get_member_1 () Const
  {
ReturnImplement->Get_member_1 ();
}

Member2 get_member_2 () Const
  {
ReturnImplement->Get_member_2 ();
}
} ;

The following is the implementation of implement. h.


Class Class_impl
{
Member1 M_1;
Member2 M_2;
Public ;
Member1 get_member_1 () Const   {}
Member2 get_member_2 () Const   {}
} ;

2. Interface Class
This is another way to create a handle class.
First, the declared class is an abstract class. Generally, the interfaces in the class are pure virtual functions, just like Java interfaces.
Then raise a static create function (that is, the factory method). This function returns the object of a specific subclass of the abstract class, and the return value in the function declaration is still the pointer or reference of the abstract class.

The specific subclass is declared in another file.

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.