Overloading External Operators with respect to Namespaces - How?

Posted on 2008-11-19
Medium Priority
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
  • 3

Accepted Solution

Stanyslaw earned 2000 total points
ID: 22996803
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

ID: 22996930
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

ID: 23002677
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

ID: 31518270
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.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

809 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