Overloading External Operators with respect to Namespaces - How?

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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevehibbertAuthor Commented:
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.
stevehibbertAuthor Commented:
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.

stevehibbertAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.