jj@idris.id.dk (Jesper Joergensen [ris]) (02/23/90)
ENVIRONMENT
===========
Compiler: G++ version 1.36.4 (configured for 'vax')
Back-end: GCC version 1.36.93 (configured for 'vax')
System: Ultrix version 2.2-1 Worksystem v1.1 System #2
Machine: DEC VAXstation 2000
SUMMARY
=======
The explicit use of a private constructor to form a class object within an
expression is silently accepted by the compiler. If the constructor is used
for initialisation in declarations or for implicit type conversion the
compiler correctly complains about the privacy.
EXAMPLE PROGRAM
===============
***** PROGRAM START *****
extern "C" int printf() ;
class Test {
private:
int member ;
Test(int member1) { // This constructor is private
member = member1 ;
}
public:
Test() { // Used for declaration
member = 0 ;
}
int print() { // Test output of member
return printf("member = %d\n",member) ;
}
} ;
int main(int argc, const char *argv[]) {
Test x ;
x = Test(3) ; // This should be illegal !!!!!
x.print() ; // See if it had any effect ...
return 0 ;
}
****** PROGRAM END ******
EXAMPLE COMPILATION AND EXECUTION
=================================
***** SESSION START *****
% g++ -v ctortest.cc -o ctortest
g++ version 1.36.4 (based on GCC 1.36.93)
/usr/local/lib/gcc-cpp -+ -v -undef -D__GNUC__ -D__GNUG__ -D__cplusplus -Dvax -Dunix -D__vax__ -D__unix__ ctortest.cc /usr/tmp/cc006773.cpp
GNU CPP version 1.37
/usr/local/lib/gcc-cc1plus /usr/tmp/cc006773.cpp -quiet -dumpbase ctortest.cc -version -o /usr/tmp/cc006773.s
GNU C++ version 1.36.4 (based on GCC 1.36.93) (vax) compiled by GNU C version 1.36.93.
default target switches: -munix
/usr/local/lib/gcc-as -o ctortest.o /usr/tmp/cc006773.s
/usr/local/lib/gcc-ld -o ctortest /lib/crt0.o ctortest.o -lg++ /usr/local/lib/gcc-gnulib -lc
% ctortest
member = 3
%
****** SESSION END ******
As you can see the assignment statement was executed and had the expected
result, though the use of the constructor is illegal.
The rest is up to you, good luck ...
Jesper Jorgensen jj@idris.id.dk
Research associate
Department of Computer Science
Technical University of Denmark
DK-2800 Lyngby
DENMARK