P
Peter Oliphant
I just found out the following code compiles:
ref class ClassA
{
public:
ClassA()
{
x = 0 ;
X_To_1( ) ;
void X_To_2( ) ; // ****
Console::WriteLine( "x = {0}", x ) ; // "x = 1"
}
~ClassA() {}
private:
void X_To_1() { x = 1 ; }
void X_To_2() { x = 2 ; }
private:
int x ;
} ;
Note the line indicated by the asterisks above begins with a 'void'. What
this does is cause this line of code to be skipped. Hence, after the
constructor runs, x = 1 and not x = 2. Is this a bug or a feature?
The way I found this is I copy-and-pasted the function declaration to the
code and didn't remove the 'void' from in front. So it's an easy trap to
fall into...
[==P==]
ref class ClassA
{
public:
ClassA()
{
x = 0 ;
X_To_1( ) ;
void X_To_2( ) ; // ****
Console::WriteLine( "x = {0}", x ) ; // "x = 1"
}
~ClassA() {}
private:
void X_To_1() { x = 1 ; }
void X_To_2() { x = 2 ; }
private:
int x ;
} ;
Note the line indicated by the asterisks above begins with a 'void'. What
this does is cause this line of code to be skipped. Hence, after the
constructor runs, x = 1 and not x = 2. Is this a bug or a feature?
The way I found this is I copy-and-pasted the function declaration to the
code and didn't remove the 'void' from in front. So it's an easy trap to
fall into...
[==P==]