C++0x11 new features: delete delete function

Source: Internet
Author: User

C_plus_plus_0x11.cpp:

//c_plus_plus_0x11.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<Windows.h>#include<iostream>using namespacestd;classa{ Public:    intAddintAintb) {returnA +b;} intSubintAintb) {returnAb;}};classD | Publica{ Public:    //int sub (int a, int b) = delete;};int_tmain (intARGC, _tchar*argv[])    {A A; cout<<"5+2="<< A.add (5,2) <<Endl; cout<<"5-2="<< A.sub (5,2) << endl<<Endl;    b b; cout<<"9+3="<< B.add (9,3) <<Endl; cout<<"9-3="<< B.sub (9,3) << Endl <<Endl; cout<<" Done"<<Endl;    GetChar (); return 0;}

Operating effect:

To delete this line of code:

// int sub (int a, int b) = delete;

Recompile, the result is:

Visible, delete can be used to delete a class from the base class inherited functions, heard can be used to delete the copy constructor, try again below.

C_plus_plus_0x11.cpp:

//c_plus_plus_0x11.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<Windows.h>#include<iostream>using namespacestd;classa{ Public: A (intAintb) {a_ = A; b_ =b;} intAdd () {returnA_ +B_;} intSub () {returnA_-B_;} //A (const a&) = delete; //no copy    inta_{}; intb_{};};int_tmain (intARGC, _tchar*argv[]) {A A (5,2); cout<<"5+2="<< A.add () <<Endl; cout<<"5-2="<< a.sub () << endl<<Endl; A b=A; cout<< B.a_ <<"+"<< b.b_ <<"="<< B.add () <<Endl; cout<< B.a_ <<"-"<< b.b_ <<"="<< b.sub () <<Endl; cout<<" Done"<<Endl;    GetChar (); return 0;}

Operating effect:

To delete this line of code:

    // A (const a&) = delete;         // no copy

Recompile:

It is estimated that delete can be used to remove a class from a base class that inherits (such as the A.sub function) and this class implicitly exists (such as a copy constructor) function.

Data Source:

C++11 FAQ Http://www.stroustrup.com/C++11FAQ.html#default

Finish.

C++0x11 new features: delete delete function

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.