nharding@extendthereach.com (Ned Harding) wrote:[color=blue]
> In VC7.1 the following code outputs:
> 00000000
> Success!
>
> It would seem that the Y::operator<< is hiding the global operator<<
> when it is outputting an object from another namespace but not when it
> is outputting an object in the global namespace.[/color]
Actually the behaviour is correct.
Also the Y::operator<< is irrelevant, the code will behave the same
without it.
[Abbreviated, equivalent code]:[color=blue]
> #include <iostream>
>
> namespace X { class Abc1 {} };
>
> std::ostream& operator<< (std::ostream& strm, const X::Abc1 *node)
> { return strm << "Success!"; }
>
> class Abc2 { };
>
> std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
> { return strm << "Success!"; }
>
> namespace Y {
> void DoTest() {
> std::cout << (X::Abc1 *)0 << "\n";
> std::cout << (Abc2 *)0 << "\n";
> }
> }
>
> int main() {
> Y::DoTest();
> }[/color]
The first example looks for:
operator<< (std::ostream &, X::Abc1 *)
in the namespaces 'std' and 'X'. It finds neither.
It doesn't look in the global namespace because none of its
parameter types are in the global namespace.
However there is an implicit conversion (X::Abc1 *) --> (void *)
so it finds std::operator<<(std::ostream &, void *).
The second example looks for:
operator<< (std::ostream &, ::Abc2 *)
in the namespace 'std' and the global namespace. It finds
your std::ostream& operator<< (std::ostream& strm, const Abc2 *node)
from the global namespace.
[color=blue]
> namespace Y {
> std::ostream & operator<<(std::ostream& a, const char& b)
> };[/color]
Why did you include that function? It has nothing to do with
anything else in this program because you never go
std::cout << (something with char type or convertible to char).