C + + Callback interface declaration __c++

Source: Internet
Author: User

When writing callback functions in Java, it is convenient to declare and implement the callback interface with interface.

The C + + declaration callback interface can declare a pure virtual class.

What is a pure virtual class.

A: A pure virtual class is a class that has only declarations that are not implemented.

Such as

Class Ainterface

{

virtual void fun () = 0;

}

Note: virtual void fun () = 0; is a declaration fun is a pure virtual function. If there is no = 0, then fun is just a virtual function, i.e.

Class Ainterface

{

virtual void fun ();

This class cannot be called a pure virtual class.


So

Class Ainterface

{

virtual void fun ();

} (Error-declaring method when implementation is not written)

And

Class Ainterface

{

virtual void fun () = 0;

What's the difference? (at present, the niche only encounters the following problem, so it can only talk so much.) )

Declare a virtual class instead of a pure virtual class in a. h file, and the virtual function needs to have an implementation, otherwise the error prompt for "unresolved external" "" is present at link.

To modify the method:

If the virtual class does not write the implementation, the following method declaration will not have this error

Class Ainterface

{

virtual void Fun () {};

(the method is declared correctly when the implementation is not written)




Related Article

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.