C ++ primer-pe13 -- 10

Source: Internet
Author: User

Summary: The value assignment operator and replication initialization are usually used together. Both of them have merged versions. Sometimes you don't need to worry about them.

In addition, private: Class (const Class &) can prevent the user code from copying this class object,

However, if you can still copy the metadata and members, you can simply declare that they are not defined. This method only prevents the result, but cannot block the call. The call will cause the link to fail.

// Then, how can we use members and friends to test the replication? It will be solved tomorrow.

I recalled the static knowledge point and used the changed static variable to identify the unique ID. for statistics, the initial count is 0 and the initial ID is 1. Therefore, the pre-auto-increment operator is used.

The problem is that the assignment operator forgets the knowledge point?

Employee EM2 = Em1; // It is equivalent to the latter.

Employee EM2 (Em1); // There is no difference between the two. Is operator = ignored here?

# Include "head. H "// ID is unique. To identify class employee {public: // constructor employee (): Name (" "), ID (++ number) {STD:: cout <"defalut-constructor:" <STD: Endl;} employee (STD: String S): name (s), ID (++ number) {STD: cout <"one-parm-constructor:" <STD: Endl;} // copy the constructor employee (const employee &); // value assignment operator employee & operator = (const employee &); // statistic and testing interface static int getcount () {return number;} int GETID () {return ID ;} STD :: String getname () {return name;} PRIVATE: STD: string name; int ID; static int number; // used to mark the ID and count }; // define the relevant member INT: Number = 0; Employee: employee (const employee & EM): Name (EM. name), ID (++ number) {STD: cout <"copy-constructor:" <STD: Endl;} employee & employee :: operator = (const employee & EM) {STD: cout <"assignment:" <STD: Endl; // id = em. ID; // different from the combined value assignment operator, idname = em can be protected. name; return * This ;} Int main () {employee Em1 ("Hu"); // initialize employee EM2 = Em1; // when the value assignment operator is not customized, the default value assignment operation does not change the ID. The custom copy ID cannot be used. This statement does not include assignstd :: cout <"==================" <STD: Endl; employee em3 (Em1 ); // copy and initialize the employee em4; em4 = Em1; // This error occurs when the non-custom value assignment operator is used. The IDS are the same. STD: cout <"ID:" <em1.getid () <":" <em1.getname () <STD: Endl; STD :: cout <"ID:" <em2.getid () <":" <em2.getname () <STD: Endl; STD: cout <"ID: "<em3.getid () <": "<em3.getname () <STD: Endl; STD: cout <" ID: "<em4.getid () <":" <em4.getname () <STD: Endl; STD: cout <"Statistics:" <em1.getcount () <STD: Endl; STD: cout <"Statistics:" <EMPLOYEE: getcount () <STD: Endl ;}

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.