Overloading External Operators with respect to Namespaces - How?

Posted on 2008-11-19
Last Modified: 2013-12-14
Hi Experts,

I have a namespace, FC, and class, String, defined in that namespace.  I have been told that, for the FC::String class to work with STL containers, I should define the binary + operator as a friend function...

    friend String operator+ (const String & s1, const String & s2);      // StringA + StringB

... and then implement this function externally, outside the class.
However, the operator+ is in the FC namespace.
When I try to implement externally...
    FC::String      operator+ (const FC::String & s1, const FC::String & s2)
        { blah blah blah code etc }
...the operator function cannot access the String class internal pointer.

How do I define the friend function so that the operator+ is defined as outside the FC namespace, but can still access the internal member variables of the class inside the FC namespace?

Question by:stevehibbert
    LVL 3

    Accepted Solution

    use the following syntax:
    friend String (::operator+) (const String & s1, const String & s2)

    Function should be forward-declared before class, and class in namespace should be forward-declared before function.

    Author Comment

    Thanks Stanyslaw, I will try this tomorrow and get back to you, I appreciate the help.  I have not seen that syntax before, very interesting.

    Author Comment

    Excellent.  It works.  For anyone experiencing a similar problem, and for myself if I need to come back to this, here's exactly what I had to do:

    First, forward declaration of the String class in the namespace
        namespace FC { class String };
    (This tells the compiler that I am going to use FC::String now, and implement later).

    Then, forward declaration of the external operator+ that uses FC::String
        FC::String operator+ (const FC::String & s1, const FC::String & s2);
    (Again, this is a prototype for later).

    Then the class is defined in it's namespace
        namespace FC
            class String
                <blah blah blah string class code>
                friend String (::operator+) (const String & s1, const String & s2); // StringA+StringB

    ...and finally the operator+ function is implemented outside the namespace (most usually in the implementation cpp file)...

    FC::String operator+ (const FC::String & s1, const FC::String & s2) {<code etc.>}

    Thanks Stanyslaw, that was very useful to me.


    Author Closing Comment

    Thanks, I could not find this anywhere in any namespace tutorials, in books or on the web.  If you have recommended text books, I would appreciate the tips, but thanks very much for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now