Re: 'void' : bug or feature?
- From: "Peter Oliphant" <poliphant@xxxxxxxxxxxxxxxx>
- Date: Thu, 19 Jan 2006 11:00:35 -0800
Oh yeah, one more thing:
>>In your example the declaration introduces the global
>>void X_To_2() function.
Are you sure about this? Note that all definitions and even usages of
X_To_2() are entirely contained inside the scope of ClassA's definition.
Further, one 'version' of MethodB is a *private* method of ClassA, the other
a 'local method' to the public ClassA constructor (e.g., if I create a
'local variable' in a constructor it is not global). Why then would either
version be global?
I assume the linker doesn't complain about methods and functions declared
and not defined, IF they are never used. Otherwise I have even more
questions... ; )
[==P==]
"Holger Grund" <holger.grund@xxxxxxxxxxxxxxx> wrote in message
news:OLbAf4RHGHA.1676@xxxxxxxxxxxxxxxxxxxxxxx
> "Peter Oliphant" <poliphant@xxxxxxxxxxxxxxxx> wrote
>
>>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"
>> }
>>
> [..]
>> 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 indicated line is simply a function declaration. Function scope
> function declarations introduce functions from the nearest containing
> namespace scope into the lexical scope. In your example the
> declaration introduces the global
> void X_To_2() function.
>
> You could write
> void X_To_2(); // declares ::X_To_2();
> X_To_2(); // calls ::X_To_2();
> ...
> }; // end of ref class ClassA
>
> void X_To_2(){ //..}
>
> Your member function declaration is in a different non-overlapping
> scope. They are completely unrelated.
>
>> 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...
>>
> There's another common pitfall. Function declarations vs local variable
> with initializers:
>
> struct Y{}; struct X{ X(const Y&);};
> void foo() {
> // Want to construct an X with a default constructed Y into variable x
> X x( Y() );
> // however, this declares a function x which returns an X and takes a
> // parameter of type Y() which is adjusted to Y(*)(), i.e.
> // a function pointer taking no arguments and returning a Y
>
> -hg
>
>
>
.
- Follow-Ups:
- Re: 'void' : bug or feature?
- From: Tamas Demjen
- Re: 'void' : bug or feature?
- References:
- 'void' : bug or feature?
- From: Peter Oliphant
- Re: 'void' : bug or feature?
- From: Holger Grund
- 'void' : bug or feature?
- Prev by Date: Re: A C# class library in a C++ project
- Next by Date: Re: how to know an int is the member of an enum type?(C++/CLI)
- Previous by thread: Re: 'void' : bug or feature?
- Next by thread: Re: 'void' : bug or feature?
- Index(es):
Relevant Pages
|