operator overload with namespace

Discussion in 'C++' started by Jason C, Feb 28, 2005.

  1. Jason C

    Jason C Guest

    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?
     
    Jason C, Feb 28, 2005
    #1
    1. Advertising

  2. 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::eek:perator + ( myfirststring, mysecondstring );

    V
     
    Victor Bazarov, Feb 28, 2005
    #2
    1. Advertising

  3. Jason C

    Jason C Guest

    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?
     
    Jason C, Feb 28, 2005
    #3
  4. "Jason C" <> wrote in message
    news:...
    > 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";
    }
    }


    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
     
    Larry Brasfield, Feb 28, 2005
    #4
  5. 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
     
    Victor Bazarov, Feb 28, 2005
    #5
  6. Jason C

    Fraser Ross Guest

    "Victor Bazarov" <> wrote in message
    news:ENHUd.48570$01.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.
    >



    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.
     
    Fraser Ross, Feb 28, 2005
    #6
  7. "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote...
    >
    > "Victor Bazarov" <> wrote in message
    > news:ENHUd.48570$01.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.
    >>

    >
    >
    > 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...
     
    Victor Bazarov, Mar 1, 2005
    #7
  8. Victor Bazarov <> wrote in message news:<ENHUd.48570$01.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
     
    Marcelo Pinto, Mar 1, 2005
    #8
  9. Marcelo Pinto wrote:
    > Victor Bazarov <> wrote in message news:<ENHUd.48570$01.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
     
    Victor Bazarov, Mar 1, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  2. Layton
    Replies:
    2
    Views:
    459
    Layton
    Sep 27, 2006
  3. mrstephengross
    Replies:
    3
    Views:
    399
    James Kanze
    May 10, 2007
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
  5. Shriramana Sharma
    Replies:
    2
    Views:
    149
    Shriramana Sharma
    Jun 27, 2013
Loading...

Share This Page