[gnu.g++] compilers and books

nagle@well.UUCP (John Nagle) (08/20/89)

You write:

> On page 275 of C++ by Stroustrup is the statement
> 
>   "No initializer can be specified for a static member, and it cannot be of a 
>     class with a constructor."
> 
> This appears to say the same thing as what Pohl writes.
> 
> The statement on page 158 of C++ by stroustrop refers to a static instance
> of a class - not about static members of classes.

      This is incorrect.  Here's an example.

	//	Class class1 - tallies instances of itself
	class class1 {			// class with static member
	static int tally;		// static member of a class
	public:
	    class1() { tally++;}	// constructor
	    ~class1() {			// destructor
		tally--;
		if (tally == 0) 
		    cout << "All instances of class1 released.\n";
		}
	}
	//	Main program
	main()
	{	class1 instance1;
		class1 instance2;
		cout << "End of main program.\n";
	}

This is quite legitimate, and, under Zortech, will compile and run, producing
the output "End of main program" followed by "All instances of class1
released."  The class class1 contains a static member, "tally", and
a constructor.   Pohl claims, incorrectly,
that this is illegal.  Strostrup says, as you point out, that 
"No initializer can be specified for a static member, and it cannot be of a 
class with a constructor."  But this is a different restriction, and a
much less inconvenient one, than Pohl conceives to be part of the language.
Pohl writes "A class with a static member cannot have a constructor."
This is wrong, and far more restrictive than actually necessary.  It's
the STATIC MEMBER that cannot have a constructor (by virtue of the member
being of a type which is a class with a constructor) not the class being
defined.

This isn't a minor point.  It is static members of classes that make it
possible for all the elements of a class to have interrelationships
invisible to the users of the class.  Consider, for example, string
packages that share duplicates, buffering packages that share buffers,
and, as Strostrup gives as an example in 5.4.4, a task scheduler that
needs to keep track of all the members of the class "task".  Without
shared static members, each instance of a class object lives alone,
isolated from its peers.  Without the combination of constructors and
shared static members, each instance cannot communicate privately with
it peers, and must be born and die in painful isolation.  The
combination of these facilities allows class instances to have rich,
useful social lives within the bounds of the society defined by their
class.

				John Nagle