Constructor semantics notes (iv)

Source: Internet
Author: User

Today this is the last note in chapter two. This section of the Members ' initialization team is recorded today.

If there is an error, please be grateful.

1. Member initialization team (members initialization list,mil):

When we define a cstor, we can initialize our members.

Two ways:1. Via Mil. 2. Inside the cstor. 2. The following points are described in this chapter: 1. When to use a mil is meaningful. 2. Internal operations during initialization3. Trap Introduction3. Four cases are recommended for use with MIL (some even must be used): 1. Initialize a reference member; 2. Initialize a const member;3. Call the constructor of the base class and have parameters;4. Call the constructor of the member class object and have parameters. 4. Explanation:explanations of 1 and 2:

#include <iostream>using namespace Std;class base{int x, y;int& rx;const int z;  Because it is const, it cannot be assigned, and cannot be left, and appears on the left of the assignment number. can only be initialized. Public:/*base (int var): X (Var), Y (x) {   //"BASE::RX": the member of the reference type must be initialized, this place is still more emphasis on initialization and assignment, the reference can be assigned before initialization is done?//rx = x;  You must use a MIL operation. }*/friend ostream& operator<< (ostream& os, const base& p) {os << "p.x =" <<p.x<< "
   
     "<<" p.y = "<<p.y<< endl;return os;}}; int main () {Base B1 (2); cout << B1 << endl;system ("pause"); return 0;}
   
The example above is easy to find.

Look at a slightly more complicated example:

#include <string> #include <iostream>using namespace Std;class word{private:string _name;int _cnt;public: Word () {_name = ""; _cnt = 0;}};/ /internal Pseudo Code: ↓word::word () {_name.string::string ();//default constructor call. String _temp = String ("");//initializes the temporary object. _name.string::operator= (_temp);///Note the place is assigned, it is not initialized. _temp.string::~_string ();//Destructors out temporary objects. _cnt = 0;}
In fact, this cost is very large, the program can be compiled and run correctly, but not recommended.

Recommended way:

Word::word (): _name ("") {_cnt = 0;}
pseudo code: ↓

Word::word () {_name.string::string ("");//Compare the above. _cnt = 0;}

To sink into:

Template<class T>foo<t>::foo (T t) {_t = t;//do it well? }
actually look at the type of T. If it's a built-in base type, it's no different, but like the string above, it's really bad.
The mil seems to steer programmers crazy with mil, even the built-in type.

5. Internal behavior:

The compiler processes the mil in the order in which you declare it, placing the appropriate initialization code within the Cstor, and the code must precede the user-defined code.

To sink into:

#include <iostream>using namespace Std;class base{int x, y;public:/*base (int var): Y (Var), X (y) {//The order of execution of this place? cout << "Using The default Cstor" << Endl;} */friend ostream& operator<< (ostream& os, const base& p) {os << "p.x =" <<p.x<<  " "<<" p.y = "<<p.y<< endl;return os;}}; int main () {Base B1 (2); cout << B1 << endl;//b1 value? System ("pause"); return 0;}
This place involves a sequence of issues, which have been described in order to process and insert code in a declaration sequence. So the X is processed first, but your x is processed with Y, and at this time y is the garbage value, so x is also a garbage value.

Slightly changed:

#include <iostream>using namespace Std;class base{int x, y;public:/*base (int var): Y (Var) {      x=y;          } */friend ostream& operator<< (ostream& os, const base& p) {os << "p.x =" <<p.x<<  " "<<" p.y = "<<p.y<< endl;return os;}}; int main () {Base B1 (2); cout << B1 << endl;system ("pause"); return 0;}
This is perfect, because the code is placed before the custom code. So the value of Y is initialized first. So the problem is solved.

The above problem is currently only g++ compiler can detect, on my vs2013 is undetectable.

Well, that's where it's going.

6. Summarize:

The mil operations are converted by the compiler, processed in the order in which they are declared, and are always placed before explicit user_code.

7. Big Summary:

This chapter is written at least one chapter, the other chapters each summary will take a few hours, and the perfectionism flooding, always want to row of exquisite points, must occupy the time. Hope that their knowledge conversion rate can be a little higher.

Summarize the next three chapters.

One chapter is the write default constructor.

The second is the write-copy constructor,

The third is to write program conversion semantics. This will not be said at the end.

Every chapter I try to write my own understanding, as far as possible to avoid copying the contents of the book, after all, not their own.

And in the first reading, some areas are still not smooth, so it is possible to read the second time, and will correct the record in this blog is not good places.

In fact, I write this blog The main purpose is to consolidate their knowledge. I read it every day, make an outline with a pen and paper, write a note, and put it in a blog post. Actually is transcription, the person is too stupid not to be able.

I'm going home tomorrow, hoping to write a blog every day. Because my goal in the next phase is to read the C + + Primer, I hope the time is ample.

End


Constructor semantics notes (iv)

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.