Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Value Lessons


It was one of those days. Bob was being his usual... um... self, Kerry the intern was practically having a nervous breakdown every time the Guru walked by (in her full act), the network was down, and Wendy -- my only link to sanity at times like this -- was on vacation. Some days it just doesn't pay to get out of bed.

Kerry and I were at his desk, reviewing some code he had been working on. I was impressed with the quality of his code -- I didn't have much to point out. (Mind you, I was pretty sure the Guru would have more to say once it was checked in and we were formally reviewing it.)

"I'm having the problem in this section of code," he said:

Ff original;
// ...
{
  Ff tempFF(original);
  // ...
}
"I'm having problems with the original, when the copy gets destroyed." I looked at the code for a moment; then following a hunch, I looked up the definition of Ff:

class Ff
{
  T t;
public:
  Ff() : pointer( 0 ) {}
  ~Ff() { delete pointer; }
  U *pointer;
  void f();
  void g();
};
"Ah, here's your problem -- you've violated the Law of the Big Three," I sat back.

"The Law of... is that something the Guru made up?"

"No," I laughed. "The Law of the Big Three is from Marshall Cline's C++ FAQ. It says that if you have any one of a copy constructor, assignment operator, or destructor, you usually need all three [1]. Just add a copy constructor and assignment operator that perform deep copy semantics, and you'll be fine."

"OK, got ya," Kerry answered.

A loud snap startled both of us. Kerry's chair squeaked almost as loud as Kerry.

"Good morning, my master," I quickly recovered. Kerry eased his chair away from me a little.

"What is this abomination?" she pointed at the class definition on the screen.

"Uh, it's OK," I stammered, unnerved by the ferocity in her voice. "He's going to add the copy constru..."

"The Law of the Big Three is a trifle, a mere gadfly. I am referring to the unholy public data member." My heart sank -- how could I have overlooked something that obvious?

"Ah, well," Kerry squeaked out, "I read in ... um... somewhere that public data members are alright for value types." I was impressed -- our little mouse was learning how to roar. The Guru regarded him coolly for a moment.

"I think, my child," she finally said, much more calmly (to my relief), "that you are confusing value types and POD -- Plain Ol' Data -- types. By definition, a POD cannot have any non-public, non-static data members. Your class contains a private member; therefore it cannot be a POD. But heed this -- a class should be either a value type, or an object type. You have created a monstrosity that is neither fish nor fowl."

"Um,... what do you mean by 'value type' and 'object type'?" I queried, a little miffed that Kerry knew something I didn't.

"A value type," the Guru spoke softly as she pushed a lock of gray hair behind her ear, "is an object that depends on its state, more than its behavior. An object type relies more on its behavior and identity, than on its state."

"Ah, I see," I replied, hoping the Guru wouldn't see through my bluffed understanding.

The Guru eyed me stonily for a moment. Some days, it really doesn't pay to get out of bed. "My child, have you so soon forgotten the blessed writings of the prophet Booch? All this is covered in exquisite detail in his tome."

"Right, of course," I said, as the penny dropped. "An object's state is... um... the current value of all its data members, right?"

"In essence, yes. Booch further enlightens us by teaching that the state of an object is the result of the cumulative operations on that object [2]."

"So how does a value type emphasize its state?"

"Its member functions focus on manipulating and presenting the object's state. Common examples of value types are classes that represent strings, dates, and so on. The prophet Henney teaches us that a value type can be substituted for another value type, with the same state [3]. He has also refined the taxonomy for object types to include Entities, Services, and Tasks."

"So, you were saying that this class needs to hide its public data. What if he encapsulates the pointer behind Get and Set functions?"

That earned me another glare. I wondered if it was possible to stay in bed retroactively. "My apprentice, you seem to be addled still from the holiday fowl you have consumed. Did I not also say that you must decide whether your class is fish or fowl? Get and Set member functions are used primarily with value types. Consider the standard string class. It is a value type, whose Get function is named c_str().

"These other member functions f() and g()," she pointed at the monitor, "are organized around the object's overall behavior and give no hint as to the internal state of the object. This characterizes the class as an object type. On the other hand, Get and Set member functions are organized around the state of the pointer. This characterizes the class as a value type."

"But surely," Kerry broke in, "it's not that cut and dried, is it?"

"Don't call me Shirley," the Guru deadpanned. Without skipping a beat, she continued. "No, my child, the design decisions are not always that clear. Indeed, the masters at Ubilab acknowledge that values are frequently implemented as objects [4]. I rather suspect that is because the, er, value of value types has been largely overlooked in the OO community. In your case, you have an object with behavior that is also trying to act as a value type.

"There is one aspect of value types that I have neglected to mention. It usually does not make sense to copy object types, because of their strong identities. Consider an Employee class -- imagine the havoc that would ensue if there were two Employees named Bob."

I shuddered at the thought. As the Guru turned and glided away, Kerry rolled his eyes at me.

"'Don't call me Shirley?'" his face contorted in disgust.

"Now you know why she's a programmer and not a stand-up comic," I said as we turned back to the keyboard.

Notes

[1] Marshall Cline. "C++ FAQ Lite," <www.parashift.com/c++-faq-lite/coding-standards.html#faq-26.9>.

[2] Grady Booch. Object-Oriented Analysis and Design with Applications (Addison-Wesley, 1994).

[3] Kevlin Henney. "Valued Idioms: Patterns for Value-Based Programming," presentation to the Denmark Design Patterns Study Group, May 2001.

[4] Bäumer, et al. "Values in Object Systems," Ubilab Technical Report 98.10.1.

About the Authors

Herb Sutter (<www.gotw.ca>) is convener of the ISO C++ standards committee, author of the acclaimed books Exceptional C++ and More Exceptional C++, and one of the instructors of The C++ Seminar (<www.gotw.ca/cpp_seminar>). In addition to his independent writing and consulting, he is also C++ community liaison for Microsoft.

Jim Hyslop is a senior software designer with over 10 years programming experience in C and C++. Jim works at Leitch Technology International Inc., where he deals with a variety of applications, ranging from embedded applications to Windows programs. He can be reached at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.