§2 Initialization of variables, const and refrescence
"Hey ... ... ..."
A phone ringtone woke me from my dreams, which is the hateful guy, I don't want others to sleep well, it is rare to do without overtime, it is really # %% ¥ # ... (here I omit 213 words complaint)
"Hey! Who?" I haven't woken up, my head is dizzy, so I am a little welcome.
"Master is early!" Young's tone is very polite.
"It turned out to be you! What did I do if I do this morning?"
"Is it very early?"
I looked at the alarm clock, "I only 7:50!"
"Ah? You didn't wake up. Look at it ..." Young deliberately dragged the last word, it seems to be a chance to correct.
I took the eyes, then I looked at the little alarm clock on the side. Oh, I turned out that the long needle and short needle were observed, and it was ten o'clome.
"Hey, sorry! I haven't awake you, I didn't wake up, last night, oh, wrong, I slept this morning. But today is Saturday, I don't have to sleep." Didn't wake up The brain is a little paste, and I can't find something a little excuse, or the truth is true.
"Oh, it doesn't matter, it seems like this, I know!"
"Well, then what do you find me today?" Or turning a topic as well.
"I don't understand what you gave me last time, so I want to ask you to have time today? Can you continue our course?" Young's tone is very sincere.
"Today? Let me see. Oh ... I have no arrangement today, or wait until the company meets?"
"Okay! That's right!"
"Okay, don't let your pigeons, rest assured!"
"Well, then I will go to the company to wait for you, you are coming."
"Ok, just like this, bye!"
Hang up the phone and hurry up.
... (30 minutes later to the company)
I entered our group of studios, I saw young already sitting on my seat, holding books, seems to be "The C Programming Language".
"Are you coming so soon? Don't you have breakfast?" I just wanted to ask her, and she had seen me and grabbed my line.
"Well, haven't eaten yet. Generally, I only eat two meals on the weekend."
"Well, wait for lunch together, or I will please!"
"Your apprentice is enough, always ask the master to eat. Well, this meal even makes me sleep, I don't blame you so early, I won't call me."
"Oh, I am sorry, I don't know so late, your old man is still in the dream."
"Old people? Cough ... Note Words! I look so old?" "None, I use this word to express my respect" Young's face, I am really, I am still Method.
"Well, then start today's courses. Well, let me think about what should you tell you today ..." When this sentence, the characteristics and keywords in the brain flashed in the brain, "We Let's talk from the basic type inside C . "
"Okay. Hey, I know that C supports all the basic types of C , just like int, char, float, double, and more. These things are the same as the original C." YOUNG is talking about these, A little confident in the eyes. "Well, basically correct."
"Ah? Is there a different?" The confident look didn't, Young entered the state of doubt.
"It is indeed different. In terms of these basic types of these C buildings, C supports the use of C , but also adds a point. I ask you, how to initialize an int type?"
Young is very confident to play on the screen:
INT A = 3; // Defines the int type variable A and initializes 3
"Correct, no matter in C and C , do you always do it correctly. However, do you know another method of initialization?"
"……do not know."
"It is actually to initialize this." I played on the screen:
INT A (3); / / Define int type variable A and initialize it to 3 with constructor syntax
"Oh, it can be like this? What is the difference between this and the original way?"
"In fact, this approach is basically no difference in the general built-in data type. But this approach has important value for C , otherwise it doesn't exist. For example, the following example. "
INT * P = new int; // Defines a pointer to the int type, and dynamically assigns a memory * P = 3; // assigns the object to which P points to 3
"There is no problem with this writing, but we can use the constructor syntax to turn the two lines of code into a line. It is like this."
INT * P = New Int (3);
"This has completed dynamic allocation and also assigned to the initial value. Remember the function of C. use to dynamically apply for a memory?"
"Remember, it is the malloc ()!"
"Yes. This function and the nature of this operator in C are the difference between NEW automatically call the object's constructor, and Malloc will not. This is also the advantage of New, it guarantees dynamically generated objects in generating When it is initialized, this feature can avoid a lot of possible errors. "
"I didn't listen to it, do you say that new is an operator?"
"Yes, New is an Operator. In fact, the operator in C is the same as the function. It is actually a special function. Well, wait until I will tell you the operator, I will give you another detailed explanation."
"Oh, roughly understand. You just said that there is no initialization when I have established an object, I have encountered it when I learned C. Well, I will remember this."
"Okay, let's go on the use of Const this keyword."
"Const? I saw it when I read the book, and I was dizzy by it. I thought that this would feel uncomfortable." Young seems to be a bit frustrated.
"Is it a key issue in the relationship with the pointer?"
"Yeah! The original master, you have a deep understanding!"
"Your mouth is really not for help, it is very powerful! Forget it, I don't care with you. I have to say a difference. In fact, I have to figure out that it is very simple. Just look at the keyword appear before *. * After it. If there is a *, the object points to this pointer is constant; if it appears in *, then this pointer itself is constant. Of course, I don't want me to have this situation before and after. Say it? "
"Hey! It turned out to be like this, this time I know!" "And, because the object of Const can't be assigned, the Const object must initialize."
"Well, remember."
"By the way, you can write a way of writing, you can directly read the relationship between const and pointers directly. Just like this
Char * const cp; // const Pointer to Charchar const * pc; // Pointer to const char
"It is easy to distinguish between these two pointers. You tried to use it from right to left."
"From right to left? I miss it? Ok, I try. The first is: 'cp const * char', the second is: 'pc * const char'"
"Well, let go, try to add 'IS A' after the variable name, then change '*' to 'Pointer to', then read it again."
"Okay. The first is: 'cp is a const pointer to char', the second is: 'PC is a pointer to const char'. Ah! This is not the difference between the two! So magical what!"
"Right, use this way to write, you can directly read the differences. For the second, you can put the const char * PC for the compiler, 'Const char * PC 'And' char const * pc; 'is the same, but the latter is easier to read for programmers. "
"This is finally figured out. I will pay attention to it when I write the program. I will know how to give others, and I will also give yourself convenient."
"Yes! I can understand this very good! Remember, 'From the perspective of software engineering, the clarity of the program is the most important!'"
"Well, remember!"
"Let's take a look at the difference and contact of the reference (Pointer). The reference can be referred to as a variable alias which represents the object that is initialized anywhere in its scope. For example,"
INT A = 3; int & r = a;
"In this subsequent code, R is the variable a. The two are exactly the same. In any operation of R, it is actually the top of A. And the pointer I think you have learned when I have learned C, I should be familiar It has its usage and characteristics. In order to make the function can change the value of the variable sent in the call, we should use a pointer or reference. For example, "
Void Swap (int & a, int & b) // utilizes a reference, swaps A and B {INT T; T = A; A = B; B = T;}
Void swap (int * a, int * b) // utilize pointers, exchange A and b {INT T; T = * a; * a = * b; * b = t;
"See the difference between the pointer and the reference? Pointer When using the variable whose points pointing, you need to use * this solution reference (Dereference) operator; and the reference is the alias of the object of its reference, it can be used directly. But It should be noted that using the characteristics of the reference, the method of changing the value of the parameter object in the function is missing, it is more readable, it is easier to error, so be careful when writing such a code. Use the reference or pointer to pass the parameters actually One advantage is to improve the efficiency of the program, avoid copying constructor and secting functions. So, often more common is the form of 'Const T & Ref' as a ginseng, T represents C a built-in type or user Customized type. In the end, it is necessary to pay attention to that reference cannot be changed after initialization, that is, the other objects cannot be referenced again, so reference is initialized when it is defined. "" Well, understand Today, you taught me a lot! Oh ... time is not early, I have to eat, isn't you hungry? "
I look at the time on the screen, God! It has been more than a little in the afternoon. At this point, my belly is also called.
"Look at you hungry! Let's take it right away!" Young seems to be hungry, always reminded me.
And I have been thinking, is it brought by this apprentice, my stomach will have a problem a day ...