Thinking about C ++ functions 1 (the default six functions), the default six

Source: Internet
Author: User

Thinking about C ++ functions 1 (the default six functions), the default six

We know that when designing C ++, the great gods will have six default functions for C ++. The so-called default is that we don't need to write them, when you instantiate a class, that is, creating an object, the compiler automatically calls the default function when using the object for data operations, however, the default functions are not intended for use by C ++. What is really powerful is that we are based on these default functions, DIY functions can achieve what default functions cannot do and what they can do. Only in this way can you increase your skill. The following are some of my thoughts when writing C ++ functions:

The six default functions are as follows:

<Pre name = "code" class = "cpp"> /************************** **************************************** * ***** Copyright (c) 2015, WK Studios ** Filename: str. h ** Compiler: GCC vc 6.0 ** Author: WK ** Time: 2015 31 5 ************************************* * *********************************/class S {public: S (int I = 0, double n = 0): m_data (I), data (n) {cout <this <"\ n";} private: int m_data; double data ;}; int main () {S s0; // 1 // The default no-argument constructor will no longer be called by the UDF compiler, // to do this, you must define a non-argument constructor or a custom default constructor. // just like the above function, that is, if there is a default value, an error is reported because the corresponding constructor S (10) is not found; // 2 S (10.8); // 3 S (int) 10.8 ); // 4S ss (10, 20); // 5S ss1 (10.8); // 6 S ss2 (int) 10.8); // 7 S (10, 10.8 ); // 8 // call the constructor S s22 = (10, 10.8); // 9 S s1 = (10, 10.8); // 10

// Six Default functions of C ++ // those functions are transparent when used, that is, those functions have learned a trick of stealth technology. Hahaha, class Default {public: /* Default () {}// Default constructor Default (const Default &) {}// copy constructor ~ Default () {}// destructor Default & operator = (const Default &) {}// value assignment operator Default * operator &() {}// accessors const Default * operator & () const {}// accessors */private: int m_Data ;}; int main () {Default t; // call the constructor Default t1 = t; // Default t1 (t); // call the copy constructor Default t2; t2 = t; // call the value assignment function Default * pt = & t; // call the value function const Default t3; const Default * pt1 = & t3; // call the const value function // call the Destructor return 0 ;}
 

The strength of C ++ is not how many default encapsulation methods are available, but the greatest space for programmers to create on the basis of satisfying these basic functions. These default functions can only ensure the normal mode of C ++. On this basis, there is a great space for interface design and implementation.

1. Let's look at a program.





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.