Borland Online And The Cobb Group Present:


March, 1996 - Vol. 3 No. 3

C++ Foundations Series - Nomenclature

by John M. Dlugosz

This article is the first in a series of discussions about the formal terminology of the C++ language.

"I don't know what you mean by 'glory,'" Alice said.

Humpty Dumpty smiled contemptuously. "Of course you don't­­till I tell you. I meant 'there's a nice knock-down argument for you!'"

"But 'glory' doesn't mean 'a nice knock-down argument,'" Alice objected.

"When I use a word," Humpty Dumpty said, in a rather scornful tone, "it means just what I choose it to mean­­neither more nor less."

­­Lewis Carroll, Through the Looking-Glass

Many people are familiar with the "When I use a word..." quote, but it's useful to see it in its proper context. This is fantasy fiction written more than a hundred years ago, yet its point is timeless­­the same kind of lexical confusion happens over and over again. One time I was discussing pointers-to-members in an online interactive conference, and it took me some time to realize that most of the participants didn't know what a pointer-to-member was.

If you're reading up on new features, or trying to gain more advanced knowledge of some existing language feature, or asking a guru for help, you're going to have to know the descriptive terms that C++ experts use. The terms often have an exact meaning in the context of C++. However, because these terms are often common words or phrases that suggest a vague meaning, the listener doesn't realize that the speaker is being precise. Worse yet, if you accidentally misname something when you ask a question, the person you're asking will perceive you meant something other than what you actually intended.

It's important to use the correct nomenclature in any continued study of C++. Toward that end, we'll present some short definitions and explanations as a continuing series, concentrating on essential terms that developers often confuse.

Declaration vs. definition

Both these terms are described in the ANSI/ISO C++ standardization committee's Draft Working Paper (DWP) in section [3.1]. A definition states everything there is to know about some symbol. All definitions are declarations. You can have a stripped-down version that provides only some information. These are mere declarations­­they are not definitions.

For example, consider these lines:

int meaning_of_life();
class X;
int meaning_of_life() { return 42; }
class X { public: void marks_the_spot      
                                   (int); };

The first two lines are declarations. You know meaning_of_life() is a function that takes no arguments and returns an int, but this line isn't the function­­it informs the compiler that such a function exists. Likewise, you know X is a class, but you know nothing more about it. X is what's known as an incomplete type.

The third and fourth lines are definitions. All definitions are also declarations. Notice how the second meaning_of_life line is a pro-per superset of the first: It also tells you the name, parameters, and return type, but now you're actually defining the function. Similarly, you now know everything about class X.

As a rule of thumb, a declaration is something less than a definition. Keep these guidelines in mind:

Formal meanings

The DWP says "a declaration introduces one or more names into a program and gives each name a meaning." Then, the DWP goes on to explain the difference between a (mere) declaration and a definition by listing everything that's a declaration but not a definition. Table A lists these items.

Table A: Items that are declarations but not definitions
Item Example
A function without a body
void foo(); 
A variable that's extern and has no initializer
 extern int y; 
A static data member inside a class
 class C  {
   int a;  //defines a member
   static int b;  //declares but doesn't define a member  
A class name declaration
class X; 
A typedef declaration, or a using declaration or directive
 typedef unsigned long ulong;

Keeping them straight

Many people understand the difference between a declaration and a definition but can't remember which is which. To remedy this confusion, let's tie the C++ meanings to meanings with which you're already familiar.

I often look up words in a dictionary, which lists each word and its definition. The definition states all the details about the word. But let's extend the analogy. In school I had a pocket spelling lexicon. This diminuitive tome listed many words but gave no other information. So, I could easily check that "scornful" is a word and "scornfull" is not. This book was merely a list of declarations for words! It's the same situation as class X above­­the compiler knows that X is a valid class, but doesn't have its definition.

Now, suppose you take a trip to debug an installation at a client site. On the way home, you're stopped by customs. "Do you have anything to declare?" they ask, as they expose the private interface of your luggage. So you tell them about the five pounds of Polish sausage that's tucked between your sweaters and backup tapes. You need to declare the fact that the sausage exists. You're not going to make the sausage on the spot, or give a detailed recipe­­you simply tell them the sausage is there. That's all there is to it: A declaration provides minimal information about something's existence, while a definition gives comprehensive details of the thing, its features, and its uses.

John M. Dlugosz is a C++ consulting expert, developer, and prolific writer. He's a member of Borland's TeamB, he leads the C++ Study Group on CompuServe, and he's the author of C++: A Guru's Handbook. You can reach John via CompuServe at 70007,4657, on the Internet at jdlugosz@dlugosz.com, or on the Web at http://www.dlugosz.com/~jdlugosz/.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.