 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Lachlan Partington Guest
|
Posted: Tue Mar 22, 2005 11:27 pm Post subject: Unexpected behaviour with operator overloading, namespaces a |
|
|
I noticed a difference in how overloaded operators are resolved
between Visual C++ 6 and VC 2003 in a particular situation involving
operator overloading, namespaces and inheritance. The new behaviour
in VC 2003 is somewhat perplexing.
Is anyone able to explain from a language perspective why the
following example is behaving correctly, or how it should be behaving
if incorrect?
namespace ns
{
struct B
{
B& operator<<(const void* v) // <----- 1
{
return *this;
}
};
B& operator<<(B& b, const char* s) // <----- 2
{
return b;
}
}
//using namespace ns; // <----- 3
struct D : public ns::B
{
D()
{
// If using declaration at 3 is defined,
// calls 2 otherwise calls 1
*this << "test";
}
};
void f()
{
D d;
// Always calls 2 regardless of whether using declaration
// at 3 is defined.
d << "test";
}
I would have thought that operator<< at 2 should always be called
(that is certainly the intended behaviour) regardless of whether the
call is made from a member function of D or not.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|