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

Virtually Misbehavin'


February 04: Virtually Misbehavin' C++ implicit overriding at work

There are days when the reality is nearly as bad as the nightmare. I was debugging some new code. Everything seemed pretty normal until a messenger alert signaled in the corner of my screen. At first I ignored it, but it seemed to become more insistent, and then I saw it was coming from Bob. The last thing I needed was an interruption from that particular direction, so I continued to ignore the alert. As I stepped through my code, the messenger alert turned different colors, and then it changed into a little hand that grew, grew, and began to reach out of my monitor and started typing on my keyboard, interfering with the code I was trying to write.

I awoke from the dream still trembling. Fumbling for the light, I resolved to cut back on the late-night debugging sessions, or the late-night pizza, or both...

Much later, when I got to work, I was only slightly fuzzy, and I felt better still after the second 20-ounce latte. I was almost human when, halfway through the morning, Wendy brought Kerry over.

"Hey, pardner," Wendy announced.

I stopped what I was doing and turned. "Oh, hi, Wendy. Hi, Kerry. What's up?"

Kerry seemed reluctant to speak up, so Wendy prodded him. "Tell him."

"I — I think my compiler has a gremlin."

My eyebrows twitched. "Do tell." Wendy smiled and watched.

"Here, let me show you my code," Kerry said. He pulled up a source file, which when simplified looked like this:

<b>#include "a.h"
// ...
class B : public A {
public:
  void f() { cout << "B::f() "; }
  // ...
};
class C : public B {
public:
  void f() { cout << "C::f() "; }
  // ...
};</b>

"Okay," I said after glancing at it. "Where's the gremlin?"

"Here in the unit tests," said Kerry, and pulled up a small test harness. Simplified, it looked like this:

</b><b>void Test( B& b ) {
  b.f();
}
int main() {
  B b;
  Test( b );
  C c;
  Test( c );
}</b>

Kerry explained: "Until yesterday, the output of this test was B::f() B::f(), which is what I expected. It's worked that way for the past year. Then, when I came into work this morning, the unit test was failing and I had an angry note on my desk..."

"From Bahb?" I asked, languidly drawling out Bob's only vowel.

Wendy interjected: "Yeah, Bahb scolded our little intern pretty soundly."

"Any ideas?" I asked.

Wendy shook her head. "I haven't noticed anything. Classes B and C haven't changed, the unit tests haven't changed, but...oh, Kerry, what's the output now?"

But I had already been rerunning the unit test, and just as Wendy asked the question the answer appeared on the screen: B::f() C::f()

"Funny," Wendy said.

"Hmm," I added.

"Gremlins in the compiler," Kerry said nervously.

"Well, it looks almost like f is suddenly behaving as though it were virtual," I pointed out.

"But it's not," Kerry said.

"Never was..." Wendy nearly said something more, but hesitated.

"Guru, what do you think?" I asked.

The Guru, who I had correctly guessed was even then silently sliding up behind us, closed her tome gently without a snap. "Greetings, my children. You have noticed the problem, I see."

"Almost. We saw the regression failure and we were just trying to figure out what the problem is."

"No," she disagreed kindly. "You have noticed the problem already. The function f is clearly not virtual...except when it is." And with a smile, she drifted away.

"Gremlins," Kerry repeated firmly.

"Hold on now, let's take a look at something here..." I looked in the source-control system and found the file a.h — and we immediately discovered that it had been updated and checked-in late last night.

By Bob.

We groaned as I pulled up the file a.h:

<b>class A {
public:
  // ...
private:
  virtual void f();
};</b>

"Bob added that virtual function last night," I said, after checking the diffs. "Okay, Kerry, do you see what's going on?"

"Bahb broke the build!" Kerry said enthusiastically, getting into the spirit of things.

Wendy shook her head. "Can't pin this one entirely on the Bahbinator, pardner."

"No," I sighed. "We can't. Bob's not actually the problem here. Nuts. Do you see the issue, Kerry?"

Kerry looked at it some more, then realized: "Is it because now there is a virtual function with the same name in the base class A, the function in class B implicitly overrides it and also implicitly becomes virtual?"

"Very good," Wendy agreed. "Specifically, A now has a function with the same name and signature. That's C++ implicit overriding at work."

"What's worse," I commiserated, "is that the virtual function in the base class is also private, but in C++ that doesn't matter. In many other object-oriented languages, a derived class can only override a virtual function in a base class if it has access to call the base version. In C++, the issues of virtualness and accessibility are completely separate."

We looked at the screen uncomfortably.

"Um," Kerry started.

"Yes?"

"Um, how do I fix it?"

We looked at the screen some more.

Finally I said: "Ask Bob really politely if he could change the name of his private virtual function. He just added it, so there probably isn't a big user community yet and it's probably less work than changing yours. But if he doesn't...you'll have to change your function's names or signatures. In B and in C."

"There's no other way?"

I shook my head. Wendy shrugged and started to leave. Kerry hesitated, then followed her, muttering, "I knew it was a gremlin" under his breath.

As they wandered away, I remembered my dream of the night before. For Kerry, Bob's fingers really had reached out of the screen and rewritten Kerry's code, with a little help from C++. There are days when the reality is nearly as bad as the dream.


Herb Sutter (http://www.gotw.ca/) is convener of the ISO C++ Standards committee, author of Exceptional C++ and More Exceptional C++, and Visual C++ architect for Microsoft. Jim Hyslop is a senior software designer for Leitch Technology International. 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.