Topic: Double scope resolution


Author: "carl.seleborg" <carl.seleborg@wanadoo.fr>
Date: 2000/01/28
Raw View
Hello,

here's a little problem of mine :

/* Top of file */
#include <iostream.h>
int x = 5;

int main(void)
{
    int x = 10;
    {    // Useless bloc
        int x = 15;
        cout << "Bloc x = " << x << endl;
        cout << "main() x = " << ??? << endl;
        cout << "global x = " << ::x << endl;
    }
}
/* End of file */

How can I tell the compiler to use the main() x? I've tried ::::x and
::(::x), but it doesn't work. Also looked in Bjarne's The C++ Programming
Language, but did not find anything for that.

If one of you has the answer, let me know it !
Thanks
--- Carl



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/01/29
Raw View
In article <86sie8$4kj$2@wanadoo.fr>, carl.seleborg
<carl.seleborg@wanadoo.fr> writes
>How can I tell the compiler to use the main() x? I've tried ::::x and
>::(::x), but it doesn't work. Also looked in Bjarne's The C++ Programming
>Language, but did not find anything for that.
>
>If one of you has the answer, let me know it !

Very simple, you cannot do it.  There is no scope resolution operator
for outer blocks.  It is assumed that you have control of a function and
so any name hiding is your problem.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 2000/01/29
Raw View
"carl.seleborg" <carl.seleborg@wanadoo.fr> writes:

> If one of you has the answer, let me know it !

The answer is simple: You can't refer to a block-local variable that
is hidden in a nested block. You'll have to chose a different variable
name for the nested variable.

Regards,
Martin



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/01/29
Raw View


"carl.seleborg" wrote:
>
> Hello,
>
> here's a little problem of mine :
>
> /* Top of file */
> #include <iostream.h>
> int x = 5;
>
> int main(void)
> {
>     int x = 10;
>     {    // Useless bloc

   int &main_x = x;

>         int x = 15;
>         cout << "Bloc x = " << x << endl;
>         cout << "main() x = " << ??? << endl;
                                   main_x

>         cout << "global x = " << ::x << endl;
>     }
> }
> /* End of file */
>
> How can I tell the compiler to use the main() x? I've tried ::::x and
> ::(::x), but it doesn't work. Also looked in Bjarne's The C++ Programming
> Language, but did not find anything for that.

Name hiding is something you have to avoid or work around. The technique
shown above is about the best you can do.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/01/31
Raw View
In article <3892EE2F.3BB735CC@wizard.net>, James Kuyper
<kuyper@wizard.net> writes
>
>Name hiding is something you have to avoid or work around. The technique
>shown above is about the best you can do.

And the best possible work around within a function is not to hide names
you want to use.  That choice is not always available when importing
global variables that may be provided in someone else's library.

>

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]