C + +: A member function that is generated by default

Source: Internet
Author: User

C + + generates 4 member functions by default.

Default constructor (constructor), destructor (destructor), copy constructor (copy constructor), Assignment function (Assignment);

Where the destructor, the basic type data for C + + is automatically destructor.

"=default" means that the default is added and can be used for overloading; "=delete" means delete, that is, default is not used;

The following code mainly shows the generation of copy constructors and assignment functions.

Code:

* * * test.cpp * *  Created on:2014.04.14 * 
 author:spike/
      
/*eclipse CDT, gcc 4.8.1* /
      
#include <iostream>  
#include <string>  
      
using namespace std;  
      
class Foo {public  
:  
    foo (std::string _name): M_name (_name) {};  
    void Say () {std::cout << m_name << Std::endl;}  
    Foo (const foo&) = default; Delete indicates deletion, default  
    foo& operator= (const foo&) = defaults;  
    ~foo () = default;  
Private:  
    std::string m_name;  
      
int main () {  
    Foo F1 ("Wang");  
    Foo F2 (F1);  
    Foo F3 = F2;  
    F2.say ();  
    F3.say ();  
    return 0;  
}

Output:

Wang  
Wang

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.