operator overload with namespace

J

Jason C

Hi, I am coming from C background and trying to learn C++. So bear with
me if the answer to my question is obvious.


I am trying to overload the "+" operator in different namespaces, like
this:


namespace A
{
std::string operator+(std::string const &a, std::string const &b);
}

namespace B
{
std::string operator+(std::string const &a, std::string const &b);
}



How do I explicitly call the + operator in either namespace, rather
than the + operator defined in the std::string?
 
V

Victor Bazarov

Jason said:
Hi, I am coming from C background and trying to learn C++. So bear with
me if the answer to my question is obvious.


I am trying to overload the "+" operator in different namespaces, like
this:


namespace A
{
std::string operator+(std::string const &a, std::string const &b);
}

namespace B
{
std::string operator+(std::string const &a, std::string const &b);
}



How do I explicitly call the + operator in either namespace, rather
than the + operator defined in the std::string?

To explicitly call an operator function you have to spell its name.

string result = B::eek:perator + ( myfirststring, mysecondstring );

V
 
J

Jason C

Thanks for the quick reply. I have another question:


if I do this:

using namespace A;

std::string s1, s2, s3;

s1 = s2 + s3;

Would the + operator defined within the string class take precedence
over my custom + operator function?
 
L

Larry Brasfield

Jason C said:
Hi, I am coming from C background and trying to learn C++. So bear with
me if the answer to my question is obvious.


I am trying to overload the "+" operator in different namespaces, like
this:


namespace A
{
std::string operator+(std::string const &a, std::string const &b);
}

namespace B
{
std::string operator+(std::string const &a, std::string const &b);
}



How do I explicitly call the + operator in either namespace, rather
than the + operator defined in the std::string?

While it is not quite explicit, in the strictest sense,
exploiting the lookup rules together with explicit
localized import of namespaces may get the effect
you want. Consider this code:

#include <string>
#include <iostream>
#include <sstream>

namespace Concat {
std::string operator+(std::string & l, std::string & r) {
return std::eek:perator+(l,r);
}
}

namespace Summer {
int operator+(std::string & l, std::string & r) {
std::istringstream lss(l), rss(r);
int il, ir;
lss >> il;
rss >> ir;
return il+ir;
}
}

int main() {
std::string lhs = "12";
std::string rhs = "34";
{
using namespace Concat;
std::cout << lhs + rhs << "\n";
}
{
using namespace Summer;
std::cout << lhs + rhs << "\n";
}
}
 
V

Victor Bazarov

Jason said:
Thanks for the quick reply. I have another question:


if I do this:

using namespace A;

std::string s1, s2, s3;

s1 = s2 + s3;

Would the + operator defined within the string class take precedence
over my custom + operator function?

This is a good question. IIRC, it will be ambiguous because the compiler
won't be able to decide between the member and the non-member.

V
 
F

Fraser Ross

Victor Bazarov said:
This is a good question. IIRC, it will be ambiguous because the compiler
won't be able to decide between the member and the non-member.


The non-member would have to be in an in-scope namespace or the global
namespace. The original post had one in a namespace which is why I'm
mentioning this.

Fraser.
 
V

Victor Bazarov

Fraser Ross said:
The non-member would have to be in an in-scope namespace or the global
namespace. The original post had one in a namespace which is why I'm
mentioning this.

I am not sure I understand your note. Jason asked a different question,
AFAIUI. Do you see the "using" directive right after "if I do this:"?
Do you think it should make any difference compared to the original
example? Just curious...
 
M

Marcelo Pinto

Victor Bazarov said:
This is a good question. IIRC, it will be ambiguous because the compiler
won't be able to decide between the member and the non-member.

V

I believe that Koenig lookup would require that the string operator be called.

Marcelo Pinto
 
V

Victor Bazarov

Marcelo said:
I believe that Koenig lookup would require that the string operator be called.

Koenig lookup only governs what functions are _found_, not what functions
are chosen from the overloaded set.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top