C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unexpected behaviour with operator overloading, namespaces a

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Lachlan Partington
Guest





PostPosted: Tue Mar 22, 2005 11:27 pm    Post subject: Unexpected behaviour with operator overloading, namespaces a Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.