473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

operator overload with namespace

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?

Jul 23 '05 #1
8 2163
Jason C wrote:
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::operator + ( myfirststring, mysecondstring );

V
Jul 23 '05 #2
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?

Jul 23 '05 #3
"Jason C" <ni********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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::operator+(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";
}
}
--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #4
Jason C wrote:
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
Jul 23 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:EN*******************@newsread1.mlpsca01.us.t o.verio.net...
Jason C wrote:
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.

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.
Jul 23 '05 #6
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:EN*******************@newsread1.mlpsca01.us.t o.verio.net...
Jason C wrote:
> 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.

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...
Jul 23 '05 #7
Victor Bazarov <v.********@comAcast.net> wrote in message news:<EN*******************@newsread1.mlpsca01.us. to.verio.net>...
Jason C wrote:
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


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

Marcelo Pinto
Jul 23 '05 #8
Marcelo Pinto wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<EN*******************@newsread1.mlpsca01.us. to.verio.net>...
Jason C wrote:
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

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
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
2
by: dirk | last post by:
Why doesn't the standard allow to overload operator new/delete in my namespace, e.g. namespace Foo { void* operator new( std::size_t sz ); void operator delete( void* address ); } On the...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.