bs@alice.UUCP (02/09/87)
***> Jim Kempf (kempf@hplabs.hp.com) writes: I am posting these questions for a friend of my, who is very shy. Please reply via. e-mail, unless the responses may be of general interest. Thank you. ---------------------------------------------------------------------- I have three questions about c++ (in particular version 1.1). Question 1: Is there any reason that initialization of static members couldn't be allowed. The version 1.1 compiler gives an error message when an initializer is present for a static member, and "THE BOOK" claims (page 275): No initializer can be specified for a static member, and it cannot be of a class with a constructor. What is the technical reason for this? I have not been able to think of any reason that this shouldn't be legal given that static variables can be initialized. ***> The point is that a class declaration typically appears in many header files and the static member may only be initialized once. To handle this correctly (assuming it was legal), an implementation must look through all .c files (or equivalently through all .o files) to find the static members that needs a constructor called and call the constructor exactly once. This was deemed impractical. (I can think of ways of doing it portably but they all cost time or space or both). Naturally, this restriction is not fundamental. Question 2: What are the semantics of a private constructor? It seems to me that a private constructor should be invisible outside of members (and friends) of a class. Yet the compiler (version 1.1) does not seem to enforce this. Is this a bug in the compiler, or am I missing something? ***> A private constructor shouldn't be invisible, just unusable. For example: class x { friend f(); x(); public: // no public constructor }; f() { x a; /* fine, f is a friend of x */ } g() { x a; /* error, x::x() is private */ } x a; /* error, x::x() is private */ This works correctly using 1.1. Unfortunately 1.1 fails to catch this one: h() { x*p = new x; /* error, x::x() is private */ } Sorry. Will fix. Question 3: The following file compiles without error under version 1.1 on our machine. It seems to me that it should complain about the arguments to the constructor since no constructor that accepts arguments has been declared. Am I missing something or is this a bug in the compiler? #include <stream.h> class foo { char c; long l; float f; friend ostream& operator<<(ostream&, foo&); }; void bar() { foo* p = new foo('a', 13, 99.9); cerr << *p; } ***> A bug. Sorry. Will fix. Note that even though the parser accepts this perfectly logical, but illegal syntax, the ``obvious'' initialization is not done.