Analysis of basic implementation methods of C ++ Using Interfaces

Source: Internet
Author: User

C ++ programming language is a very useful application language for developers. However, there are still a lot of advanced content that we should spend a lot of time learning. Here we will first introduce the implementation of the c ++ interface.

Object-oriented languages such as JAVA provide interfaces to implement interfaces, but C ++ does not. Although C ++ implements interfaces through pure virtual base classes, for example, the C ++ Implementation of COM is implemented through the pure virtual base class. Of course, the COM Implementation of MFC uses Nested classes), but we prefer to see something like Interface. The following describes a solution.

First, we need some macros:

 
 
  1. //  
  2. // Interfaces.h  
  3. //  
  4. #define Interface class  
  5. #define DeclareInterface(name) Interface name { \  
  6. public: \  
  7. virtual ~name() {}  
  8. #define DeclareBasedInterface(name, base) class name :  
  9. public base { \  
  10. public: \  
  11. virtual ~name() {}  
  12. #define EndInterface };  
  13. #define implements public 

With these macros, we can define our interfaces as follows:

 
 
  1. //  
  2. // IBar.h  
  3. //  
  4. DeclareInterface(IBar)  
  5. virtual int GetBarData() const = 0;  
  6. virtual void SetBarData(int nData) = 0;  
  7. EndInterface 

Isn't it like the macros for message ing in MFC? friends who are familiar with MFC must be familiar with it. Now we can implement the function of using the C ++ interface as follows:

 
 
  1. //  
  2. // Foo.h  
  3. //  
  4. #include "BasicFoo.h"  
  5. #include "IBar.h"  
  6. class Foo : public BasicFoo, implements IBar  
  7. {  
  8. // Construction & Destruction  
  9. public:  
  10. Foo(int x) : BasicFoo(x)  
  11. {  
  12. }  
  13. ~Foo();  
  14. // IBar implementation  
  15. public:  
  16. virtual int GetBarData() const  
  17. {  
  18. // add your code here  
  19. }  
  20. virtual void SetBarData(int nData)  
  21. {  
  22. // add your code here  
  23. }  
  24. }; 

It's easy. We don't need to make a lot of effort to implement the C ++ interface operation.

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.