//Purpose: to see if certain kinds of statements compile. //The compiler seems to err where "+++" text is found, below. //From _The C++ Programming Language_, 3rd ed., Stroustrup, p. 228, section //"10.2.4 Static Members:" //"... a [design] that preserves the semantics of default constructor values" //... "without the encumbrance of a publicly accessible global variable." //Tested on platform DJGPP ver. 2.03 using compiler G++ ver. 2.953 by default //"tuned for Pentium" on an Intel 386 running Windows 95 ver. 4.00.950. //Author: George Sawyer (address@hidden). //Date: July 19, 2001. #include #define SHOW_ERROR //Class to test compiler. class AAA {private: int data1; int data2; //Default value: data member declaration. static AAA defaultValue; public: //Constructor declaration, with its own defaults. AAA ( const int & d1 = 0, const int & d2 = 0 ); //Set default value: member function declaration. #ifdef SHOW_ERROR //Error (1) calling this, when function was void. static void setDefault ( const int & e1, const int & e2 ); #else //Solution was to use: static int setDefault ( const int & e1, const int & e2 ); #endif //Obtain object values: member function declarations. int out1 ( ) const; int out2 ( ) const; }; //Constructor definition, with its own defaults. AAA::AAA ( const int & d1 = 0, const int & d2 = 0 ) { //If values not supplied, use stored defaults. data1 = d1 ? d1 : defaultValue.data1; data2 = d2 ? d2 : defaultValue.data2; } //Set default value: member function definition. #ifdef SHOW_ERROR //Error (1) calling this, when function was void. void AAA::setDefault ( const int & e1, const int & e2 ) #else //Solution was to use: int AAA::setDefault ( const int & e1, const int & e2 ) #endif { defaultValue = AAA ( e1, e2 ); #ifndef SHOW_ERROR return 0; #endif } //Obtain object values: member function definitions. int AAA::out1 ( ) const { return data1; } int AAA::out2 ( ) const { return data2; } //----End of class definitions.---------------------------------------- //Define and initialize default value: use constructor, abbreviated form. AAA AAA::defaultValue ( 10, 11 ); //Obtain default value: use constructor, normal form. AAA var1 = AAA ( ); //Generate particular value: use constructor, normal form. AAA var2 = AAA ( 20, 21 ); //Generate particular value: use constructor, abbreviated form. AAA var3 ( 30, 31 ); //Using appropriate member function, set new default value. #ifdef SHOW_ERROR //+++Error (1) here, for some reason. Compiler says: //+++ANSI C++ forbids declaration `setDefault' with no type. //+++`int AAA::setDefault' is not a static member of 'class AAA'. //+++initializer list being treated as compound expression. AAA::setDefault ( 40, 41 ); #else //Solution was to use: int n = AAA::setDefault ( 40, 41 ); #endif //Obtain default value: use constructor, abbreviated form. #ifdef SHOW_ERROR //+++Error (2) below, for some reason. Compiler says: //+++Request for member `out1' in `var4', which is of non-aggregate type //+++`AAA ()()'. //+++(Same for out2.) AAA var4 ( ); #else //Solution was to use normal form: AAA var4 = AAA ( ); #endif int main ( ) { cout << "var1 is " << var1.out1 ( ) << ", " << var1.out2 ( ) << ".\n"; cout << "var2 is " << var2.out1 ( ) << ", " << var2.out2 ( ) << ".\n"; cout << "var3 is " << var3.out1 ( ) << ", " << var3.out2 ( ) << ".\n"; //+++Error (2) here, explained above. cout << "var4 is " << var4.out1 ( ) << ", " << var4.out2 ( ) << ".\n"; }