Conversation #26: A Midsummer Night's madness

Source: Internet
Author: User
Conversation #26: A Midsummer Night's madness

"Well, let's just say, will you have a moment ?"

I recognized the timid voice from behind me. Kerry, a new employee in our department, is standing in my room. He is smart, ...... Man, funny. I once told Wendy that, but she only muttered to herself, probably because of basin and pot problems.

"Yes, no problem," I turned to him. I don't have much experience in Kerry, but I feel comfortable as a veteran. I prepared him to give me everything.

"What is our London office hotline number ?"

I sighed. It is really troublesome to become a guiding expert. I wrote a number on the notepaper and handed it to him.

"Thank you," he turned and said. "I have to ask Bob something ."

Dangerous, Robinson! My heart has raised my blind eye. "No !" I blurted out.

Kerry crashes. "That's to say, don't bother him ?" "I mean, well," I thought about it ...... Think fast. "Don't bother him. I'm sure we can handle it ."

"But Bob said I should be away from you," Kerry said in a sticky manner. "Before he left, he said that if I had any questions, he should call him. He said, 'No matter what you do, never ...... '"

"Oh, look at the time," I interrupted. "Now London time is over. Bob should have been away for a long time ."

"But now it's only ."

"Yes, this is Bob's off-duty time, I think ." I shouted, "Bob ...... When he was in London, he had to work earlier ."

"Oh. Okay, I think that makes sense, "Kerry seems uncertain, but he is still sitting in my guest chair.

"The problem is that I have a function that receives parameters of the metric type," he says on the whiteboard:

Typedef T * tptr;

Void F (const tptr T)

{

* T = T (); // don't want this, but it's allowed

}

"In the function body, I can modify t even if I declare it as Const. I also tried tptr const t, but it does not work either. If typedef is not used, I know that I can write const T * T, which is what I want ."

"Ah," I thought about it. I know that I have encountered this problem before.

But where? When? "Well, the text replacement of typedef and macro is not exactly the same. I can't remember all the details, but I'm sure that when the type is followed by the const modifier, its behavior is the same as your expectation ." I slightly modified the Code:

Void F (tptr const t) // still means T * const

Kerry looks at the new code. "But tptr const T and const tptr t are not the same. Isn't it ?"

A new voice says, "No! ."

I am vibrating by the sound of Guru behind me. Kerry shouted and jumped up from the chair. Guru continued calmly, closed his thick book, and turned a blind eye to our response: "Tell me, young man, what is the difference between int const I and const int I ?"

Kerry's eyes were wide-so big that I was so curious about how Bob described guru. Guru does not seem to notice his trembling.

I think it is better not to participate in the actions of guru now: "They are the same thing. Isn't it, Kerry ?" I am prompted. Kerry just nodded and didn't talk.

Guru frowned. "So, if the const int and INT const are equivalent, but expect the tptr const and const tptr to be different, do you think so? Hmm? You shouldn't think so ."

"Yes, I'm explaining that part," I added a note. "Okay, Kerry, in this code, what is const ?" I wrote:

Const int I; // or Int const I;

"Int," Kerry said slowly. This simple problem brought him back to peace.

"Okay," I nodded. "In this code, what is const, even if T is a typedef ?" I wrote:

Const t; // or t const t

"T object," Kerry replied. Now we are more confident.

"Okay. In this Code, what is const ?" I wrote:

Const tptr t; // or tptr const t

"That ......" Kerry swallowed up. "Oh. Tptr ?"

"Yes. However, tptr represents a composite type, while const is only a part of this type. The pointer itself is const; this is the same as T * Const. Now there is a little more: in this code, what is const ?" I wrote:

Const T * t; // or t const * t

"That ...... The object to which it points, "he called it out.

"Yes," I affirmed, and put down the writing pen. "This is the difference between const tptr and const T ."

Guru put a hand on my shoulder and took over the topic: "In fact, this is one of the reasons why some precognition [1] made the following suggestions: without changing the meaning of the statement, place the const keyword on the right as far as possible. Then you expand typedef in your mind as an evil macro, "she paused," to get the correct meaning ."

"Yes, but I always think it is a bit difficult," I complained. "I like to declare 'const int * iptr', so that when I read it, I can read it as 'int type pointer iptr' of a constant '. Almost everyone and all books write it like this. In addition, this method is quite easy to understand ."

"What is the interface ?" Guru raised his eyebrows. "Although we neither recommend nor oppose the style of 'const on the right', doing so is indeed advantageous. It makes the read Statement easier ."

"How ?" Kerry asked.

"You must read it in the way used by the compiler. You are actually saying that 'iptr is a pointer to the const int ', and from the right to the left is the true meaning of 'int const * iptr ."

"But, how do I declare my function so that the T object is actually const ?" Kerry goes back to his initial issue.

"Ah, my child, very simple ." Guru writes:

Void F (const T * t)

"But I really want to use typedef," Kerry opposed.

"Ah, wipe the board. If typedef is required, define another typedef for the required const T :"

Typedef const T * tcptr;

Void F (tcptr T)

"But still be careful," guru's words indicate a bad omen. "We have found that the simple use of typedef produces a very small pointer value. Because of the const defect discussed here, two separate typedef must be provided. This kind of typedef proliferation, and the difference in the sense is very small, will only cause confusion. There is no value at all ."

Guru opened her book again and turned and left. "Chaos is the dark side of programming. Too many typedef and powerless names-These all lead ......" Her voice disappears at a certain corner.

I checked Kerry. He stared at me for a long time.

"Don't worry," I comforted him. "You'll get used to her. She is really a very good programmer ." Kerry blinked, opened his mouth and closed, and walked away with a blank face. I shrugged and went back to work.

[Comment]

[1] notably Dan Saks.

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.