Implementation of C ++ interface ing

Source: Internet
Author: User

The C ++ interface is used to define a program protocol. The class or structure that implements the interface must be strictly consistent with the interface definition. This article describes how to explicitly implement C # interface members...

Ing, which indicates a one-to-one correspondence function. The meaning of C ++ interface ing is the same. interfaces are implemented through classes. Therefore, for each member defined in an interface, it should correspond to a member of the class to provide specific implementation for it.

The following conditions must be met:

◆ If both A and B are member methods, the names, types, and form parameters of A and B should be consistent with the number of parameters and the type of each parameter.

◆ If A and B are both attributes, the names and types of A and B should be consistent, and the accessors of A and B are similar. However, if A is not an explicit interface member execution body, A can add its own accessors.

◆ If A and B are both time-based, the names and types of A and B should be consistent.

◆ If both A and B are index indicators, the types of A and B and the number of parameters in the form parameter table should be consistent with that of each parameter. In addition, the accessors of A and B are similar. However, if A is not an explicit interface member execution body, A can add its own accessors.

So how can we determine which class member is implemented for a C ++ interface member? Which class is mapped to an interface member? Here, we will describe the interface ing process. Assume that class C implements an interface IInterface, and Member is a Member of the interface IInterface. The ing process of the interface Member is as follows:

1. If there is an explicit interface Member execution body in C, which corresponds to the interface IInterface and its Member, it is used to implement the Member.

2. If condition 1) is not met, and C contains a non-static public Member, which corresponds to the interface Member, then implements the Member.

3. If the above conditions are still not met, search for a base class D of C in the base class list defined in Class C, and replace C with D.

4. Repeat steps 1-3 to traverse all direct and non-direct base classes of C until a member of the class that meets the conditions is found.

5. If not found, an error is reported.

The following is an example of calling a base class method to implement interface members. Class Class2 implements the C ++ interface Interface1, and the base class Class1 of class Class2 also participates in interface ing, that is, when class Class2 implements interface Interface1, the member method F provided by class Class1 is used to implement the member method F of interface Interface1:

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of 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.