Handle Class & Interface Class

Source: Internet
Author: User

Both are used to reduce compilation dependency between files.

1. Compilation dependency

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 is changed or the implementation of class_name in file is changed, all files are included. 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 only make small changes to class_name.

 

2 handler class

Handle class only improves all interfaces and contains a pointer to the real implementation class. The real implementation class is included in another file. When the class is modified, only file. H will cause re-compilation, and include file. Other files of H will not be re-compiled

# Include "file1.h" // contain member1
# Include "file2.h" // contain member2
# Include "Implement. H"

Class class_name
{
Class_impl * implement; // shared pointer is usually used.

Public:
Member1 get_member_1 () const
{
Return implement-> get_member_1 ();
}

Member2 get_member_2 () const
{
Return implement-> 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, declare that the class is an abstract base class. Generally, the interfaces in it are pure virtual functions, and then provide a static create function. This function returns the object of a specific subclass of this abstract class, the return value in the function declaration is still a pointer or reference to this abstract class. The specific subclass is declared in another file.

Class base
{
Public:
Virtual ~ Base (){}

Void dosomething ()
{
Stepone ();
Steptwo ();
}
PRIVATE:
Virtual void stepone () = 0;
Virtual void steptwo () = 0;
};

Class derived: public Base
{
PRIVATE:
Virtual void stepone ()
{
Cout <"derived stepone: Do something." <Endl;
}
Virtual void steptwo ()
{
Cout <"derived steptwo: Do something." <Endl;
}
};

 

Handle Class & Interface Class

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.