Conversations: Value Lessons (value type)

xiaoxiao2021-03-06  105

Conversations: Value Lessons (value type)

Jim Hyslop & Herb SUTTER /

Liu Weipeng / translation

When can you learn to pay attention to the advice of the prophet? Judging what type of object you define and always don't.

One day, Bob is the same as it is ..., and Kerry, poor intern, whenever Guru will always fall into a nervous nervous, the network is hung, Wendy - Whenever this time can be found in this time It is also a clear person - but is on vacation. Sometimes you will find that it is really not cost-effective.

Kerry and I are watching some of the code he wrote at his desk. I am surprised by the quality of the code - I can't find a place that I want me (but it can be sure that "the code will definitely find problems after these code after being check in).

"The problem is in this section" he said:

FF Original;

// ...

{

FF Tempff (Original);

// ...

}

"When the copy of the object is destroyed, the original object has a problem."

I saw the code for a while, suddenly there is a premonition - I found the definition of FF:

Class FF

{

T t;

PUBLIC:

Ff (): Pointer (0) {}

~ Ff () {delete Pointer;}

U * Pointer;

Void f ();

Void g ();

}

"Ah, your problem is here - you violate the Law of the Big Three ''" I re-put it back on the chair.

"Three ..., is that the guru doned?"

"No," I smile "'three law' from Marshall Cline C FAQ. Say: If you have any copy constructor, assignment operator, or the destructor, then you usually need them. Three. Now, add the copy constructor and assignment operator, and perform the correct deep copy, the problem is solved. "

"Ok".

Suddenly, a voice was shocked, Kerry's chair called almost as loud as Kerry.

"Good morning, master", I quickly reacted, Kerry took his chair from me next to me.

"What is these hate things?" She pointed to the class definition on the screen.

"Oh, no ... Nothing ..." I made a knotted Baba, in her tone, "he is preparing to join the copy structure ..."

"The three laws" is a small thing, I refer to that dirty public data member "My heart is sinking - how can I forget such a obvious thing?

"Ah, okay ..." Kerry called "I am ... Hey, some places ... read, the value type, the public data member has no problem". I am surprised - our little mouse is learning how to roar. Guru looked at him for a while.

"I think, my apprentice" finally opened, more calm (I am relieved) "You confuse the value type and POD type, from the definition, POD can't own non-public, non-static data Members. Your class contains a private data member, so it's not a POD type. But pay attention - a class either value type or is an object type. So, you created a 'quad inexplicer'! "

"Hey ... What you said 'value' and 'object type' refers to ...?" I asked, I know some things I don't know about it. I don't know something.

"A value type is dependent on its state, but not behavior" Guru said gently, after a gray hair is closed, "and an object type is more dependent on its behavior and identity, but not State. "" Ah, I understand "I replied, and I praying that Guru didn't know what I pretend to understand.

Guru saw me for a while. (Sometimes you will find that it is really not worth it). "My apprentice, do you forget the prophet Booch? These have an elegant narrative in his most head."

"Yes, of course," I have a voice "" The status of an object is ... ... is the current value of all of its data members, right? "

"Essentially, BOOCH also led us that the status of an object is the result of applying a cumulative operation of the object."

"So how do you emphasize its status?"

"The value of the value of the value focuses on the manipulation and the state of the object. Typically, the value type is String, Date, etc. Prophet Henney teaches us that the value of the same state can be replaced with each other. He also refine the object type Syarctory, Entities, Services, and tasks (TASKs). "

"So, that is, this category hides its public data - how is it hidden behind the GET and SET functions?"

This sentence has made me a white eye. I really hope that I can go back in the nest.

"My apprentice, looks like the killing of the holiday makes you stupid. I still said that you have to judge your class" Is it still the horse "? GET and SET member functions are mostly values Type. Consider std :: string, it is a value type, its Get function is named c_str () ".

"F () and g () These functions include" she pointed to the display "in the behavior of the entire object and does not give any hints in the state inside the object. This means that the class is an object type. On the other hand, The GET and SET functions represent the state of the member data Pointer, which in turn shows that the class is a value type. "

"But" Kerry plugs in "reality is definitely not so clear, isn't it?"

"Don't call me Shirley" Guru, "Yes, my apprentice, design choice is not always so obvious. In fact, Master UbiLAB recognizes usually realized in the form of objects. I have some doubts this Due to the value of the value, the value of the value is largely ignored. In your code, you have an object, but its behavior is trying to get close to a value type. "

"For the value type, there is a place I forgot to remind. Usually, the copy object type (object) is there is no meaning, because they have a strong uniqueness. Consider an EMPLOYEE class - if there are two What is the name of EMPLOYEE? What is BOB caused by BOB?

I feel cold and chestnuts for this idea. Guru turned around and quietly walked away. Kerry turned his gaze to me.

"Don't call me shirley?" His face was distorted.

"Now you know she is just a programmer, but not comedian," I said.

转载请注明原文地址:https://www.9cbs.com/read-100621.html

New Post(0)