(WQ language, although "seeking all right,", can still be touched to make this topic, it is because it is too exciting, it is too inspirated.)
Dialogue # 08: Access restrictions
boom.
The loud sound came from the distance. It feels more difficult than hearing. The excavation team started again.
"I hope ..." Jenny only opened his head and didn't sound.
"I know." I buried my head. After a while, I added a sentence: "I also hope to send a letter to my home. But the communication system must quickly resume work."
"They work," Jenny said.
Boom, another loud noise.
"Yes, but because the system of the round house has been destroyed, it is limited to use only in an emergency. We are not authorized now. This is reasonable." I replied.
Another silence. Finally, Jenny low voice: "Ah. Well, they are saying this, but isn't this situation now?"
"Yes. This is just temporary. I don't like this, but you will definitely don't think they are ..." Now I have no sound.
boom.
"boom".
This is the content written on the note on my monitor. Yes, there is also a file name I know this file - I will add a tool class in the Project. The notes are written in a lead method with Guru, but I don't understand what it means. Guru is not there, I can't ask her.
At this time, Wen Di, the programmer I compartment, came in, and sat in her position. I explore the head and expect her to understand the meaning of the note. She looks very embarrassed, the head is in the hands. "You seem to have a crazy weekend," I take the game.
She nodded weakly. "On Friday, I returned home, carbon monoxide detector in the alarm - gas in leakage. We have to spend a weekend in a friend, until it is repaired. I have almost no eyes - I have been thinking, if I have not installed the detector What [1]. "
"Oh, it's really a thousand," I sympathetically, and write down on paper to check my detector carefully. "Everyone is ok?" Ten Di nodded. I think she needs to disperse thinking, so I mention this note to her and ask her what it means.
"Wait," she replied, "GURU left the note on your monitor?" I nodded. "My Friend," she said, "It sounds like you as a Bob."
I am shocked. "You are joking!" I whiten my face. Bob Was The Worst Programmer EVER Bob has been the worst programmer, I have a chance to attack him and his worse programming style.
Weidi shaken her head and her face was serious. "No. He has always been the only person who received this mysterious note. If you receive one, you will definitely have a bad child. Let's take a look at the source file." She turned to the computer, logged in and called out. Problem file.
I have moved a chair. "In my opinion, everything is no problem," I saw it from her shoulders while I said.
Weidi moved slightly - she was more focused more than I expected. "There is no problem under thick look," Weidi agreed. "Maybe and the previous version will find some clues." I watched her use of the version control system to display the difference between the two versions. I noticed that - I still tried to master the most basic checkin / checkout directive, and at this time she immediately called a version comparison command. I am still trying to classify the command of Wendi.
"Yes, this is some saying, but it is very important," she pointed to the screen. "You changed the data member of the class."
I am going to the screen. "Yes, I adjusted its implementation small, I will identify 3 separate Double Double Double Double of the object position into a Double array. It makes some member functions more efficient. But these members are private, have you seen? ? "
Class Point
{
Private:
Double Location [3]; // x, y, z coordinates.public:
Void SetLocation (double x, double y, double z) {
Location [0] = x;
Location [1] = Y;
Location [2] = z;
}
Void getLocation (Double & X, Double & Y, Double & Z) {
X = location [0];
Y = location [1];
z = location [2];
}
}
Just then, I noticed that the comparator displays an empty line on the top of the file to be modified. "Why is this a line?" I couldn't yell.
"Maybe it is a variation of space," Weidi guess. "So let us set it to ignore spaces." After some keystrokes, then ... this line is still in a state. I am positioning the cursor to this line and press the "End" button - the happiness thing will appear. It is a statement that indent to the rightmost side, is this statement:
#define private public PUBLIC
Weidi and I looked at each other. "Bob!" We tried to.
Bob, just at this time, I am coming to us. Guru is not the only person who "said Cao Cao to Cao Cao". "Hey, new," he said, "I am just a way, just come and see if you have changed the problem."
I closed my eyes and dozens. slowly. Then, again zero. I raised a note: "What is your question?" I asked told.
"Hey. Yes, it is this. She is inexplicably left on my monitor. When you change X, Y, Y and Z to an array, it mids me most of the code, so I will pass it. You. Change it, don't worry from now, you can, new. "
"Bob, what does this #define statement mean?" I pointed to the row of annoyed code.
"Oh this," Bao is laughing. "Cool, isn't it? My code needs to access relevant data directly. Efficient, y'Know you know. At first, I only use this #define statement in my own code, but I have been using it so much, so I naturally add it to your header file. I have to handle a lot of data operations, so I can't withstand the overhead of the access function you provide. But when you replace separate Double, I use the array, my code 'Boom'. "
"Bob, actually." It seems that we have heard the voice of Guru. We all jumped. She pushed the glasses on the nose with her finger. "Moreover, you have the rule of the programming principle" She accuses it, "is the reason I left on your monitor. You try to disturb the behavior of access control breaks the package. You will change the private to public behavior in your code. Violation of One Definition Rule. You must change your code to use access functions. "
"I have already said, I can't bear the overhead of the visit function, dear," Abbao argued. "I must-"
"Don't say it!" Guru interrupted him "Access function is inline, will not cause overhead." Bob tried to refute, but Guru blocked him. "So, in order to avoid all the rumors [2], we use tests to prove the actual price of the access function. During this time, the code that is not as good is cleared. Then study and think about 1 meyers 20 [3]."
Bob has done his own room. Guru stood quietly until he had gone, and then I received my own room.
"The apprentice," she said softly, "Please write a test code to calculate the overhead of the direct access, compare the inline access function and the non-INLINE access function. To ensure that the non-INLINE access function is processed in other compilation units Medium .While you are at it, an inline operator [] would Be useful. And the Operator [] of Inline will help. "I thought. "Do you mean this?" I wrote:
Class Point
{
PUBLIC:
Inline Double Operator [] (int index) const {
Return Location [index];
}
}
"Very good, apprentice. You remember the correct use of Const. But now, this writing it will not just an access function, there is a very good Mutator function. What can you get from this function? benefit?"
I thought for a while. "You can check the crosstalk on the subscript." I have modified:
Class Point
{
PUBLIC:
Inline Double & Operator [] (int index) {
askERT (INDEX> = 0 && index <= 2);
Return Location [index];
}
}
"Hey, beautiful!" This, I like it. "This critical examination does not have any performance overhead in the Release version."
Guru nodded. "And," she said, "When using an optimized compiler, it can generate and directly access the members as the same efficient code, and combine the package and their security."
"So," I think, "There is no reason to have public data members, is it?"
Guru stopped before answering. "In most cases, there is no reason. However, if a class is just a convenient data aggregate, not an object model - that is, the C style Struct, only data without extra behavior - then put all Data is equipped with rational [4]. "
"You can expose private members to the private member through access function and the Mutator function, but only when it is necessary. Excess GET and SET functions - including Operator [] - indicate that you didn't care carefully. When accessing the function is really overhead, you can improve the speed by implementing them as inline. Of course, the resulting increase is different from the compiler - so you need to test. "
"I understand, boss," I am happy. She laughed, and she left her hand quietly. I sat down and write the test code. As a result, at least the compiler I used, very interesting [5]:
Approximate Run-Time in MilliseConds
IMPLEMENTATION
Optimizations Off
Optimizations on
1: Direct Access
1061
251
2: Inline Accessor Function
1673
240
3: OUT-OF-Line Accessor function
1662
1432
boom.
"I don't like it," Jenny repeated. "They said that the round house was destroyed because of an unexpected internal explosion, so we can't go there, but the work is still going, there is a higher license to be allowed to enter and have not come. They say that the distance communication facility is Destruction, and define only for use in an emergency, but the only result I can see is that we cannot contact any other person near the Nikitar space. "
"is it?"
"I don't like this." Boom.
We didn't speak for some time, and the roar of the end is still stopped, but the silence is more uncomfortable than the irritability. After a while, Jenny frowned and standed.
[Note]
[1] Note from the authors: this situation actually happened to me (Jim) recently I urge all readers to install carbon monoxide and smoke detectors and ensure they are well-maintained I know it's the best investment I've ever made..! Contact Your Local Fire Prevention Office for Information and Assistance.
[2] "Premature optimization is the root of all evil" - Donald Knuth Depending whom you believe, he said it in one or more of:. "Structured Programming with go to Statements" (Computing Surveys, Vol 6, No. 4. , December, 1974, Page 268), Literate Programming, or Computer Programming as an Art (1974).
[3] Scott Meyers, Effective C , 2nd Edition, Item 20: "Avoid Data Members in The Public Interface" (Addison-Wesley, 1997).
[4] C FAQ Lite, Marshall Cline, http://www.parashift.com/c -faq-lite/classes-and-Objects.htm#[7.8].
[5] The Source Code and Full Details of this Test Are Are Available on The Cuj Website At Hyslop.zip.