Friend functions

This is a discussion on Friend functions within the C++ Programming forums, part of the General Programming Boards category; In my header file I'm declaring some members in this way: Code: private: int tempLength; public: friend String operator+(const String ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Friend functions

    In my header file I'm declaring some members in this way:

    Code:
    private:
        int tempLength;
    public:
        friend String operator+(const String &, const String &);
    Then in the .cpp file I access them this way:

    Code:
    String operator+(const String &tempStringOne, const String &tempStringTwo){
        tempLength = ....
    }
    I'm getting an error everytime I try and access tempLength. The compiler says it is undeclared. I thought making the function a friend it would have access to the private data members? Or am I not understanding it?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    should be
    Code:
    String operator+(const String &tempStringOne, const String &tempStringTwo){
        tempStringOne.tempLength = ....
    }
    because this operator is not a member of String.
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    In my book it says "By making a function a friend to a class, you allow the function the same access privileges that a member function of the class has."

    Then it says to declare it the way I did in the header and then in the .cpp to exclude the String:: and the friend keyword. So even though it's not a member of String it still has the same access privileges of a member. But it's not letting me access them.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    What have you tried, given ZuK's advice?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    to access the member tempLength of a String you have to have an object of type String.
    Kurt

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by laserlight View Post
    What have you tried, given ZuK's advice?
    Well the thing is I have to keep it as a friend function. I can't use the String:: at the beginning of it because then it won't let me take two arguments because it automatically takes the "this" object and I need to take in the two arguments. So I'm not really sure what to do. I know it's not a member function because I declared it a friend function. So his hint didn't help me much. I'm thinking the friend function can't access the private data members but my book says it can but it doesn't specifically show code that does it.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by ZuK View Post
    to access the member tempLength of a String you have to have an object of type String.
    Kurt

    Shoot. Well I'll show you the rest of that code.

    Code:
    String operator+(const String &tempStringOne, const String &tempStringTwo){
           
           
           length = tempStringOne.length + tempStringTwo.length;
           buf = new char[tempLength];
           strcat(buf, tempStringOne.buf);
           strcat(buf, tempStringTwo.buf);
           
           String tempString(buf);
           delete [] buf;
           
           return tempString;
           
           }
    So I kinda got what you were saying. I used "length" right two out of three times. So I would have to say "int length" and "char * buf" here instead of what I have. That kind of sucks. I think I'll just make the new object at the beginning instead of at the end and then update it's variables.
    Thanks for the help.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    I think I'll just make the new object at the beginning instead of at the end and then update it's variables.
    You could do that, but an alternative is to implement an member function operator+=, then implement a free function operator+ in terms of it. An untested example:
    Code:
    String& String::operator+=(const String& rhs) {
        int templength = length + rhs.length;
        char* tempbuf = new char[templength + 1]; // Account for null terminator.
        tempbuf[0] = '\0';
        strncat(tempbuf, buf, length);
        strncat(tempbuf, rhs.buf, rhs.length);
        delete[] buf;
        length = templength;
        buf = tempbuf;
        return *this;
    }
    Now you can just write:
    Code:
    String operator+(const String& lhs, const String& rhs) {
        return String(lhs) += rhs;
    }
    Note that operator+ no longer needs to be a friend function.
    Last edited by laserlight; 04-22-2007 at 01:37 PM. Reason: Spotted a possible mistake in the code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    >> I can't use the String:: at the beginning of it because then it won't let me take two arguments because it automatically takes the "this" object and I need to take in the two arguments.

    ZuK didn't advise you to use String::. His sample code tells you exactly what you need to do to access a length variable of an object. But you are already doing that correctly, you just didn't declare the temporary length variable you are using to store the sum. It is just a variable by itself, not a member of String. You need to declare it as an int.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I understand that now. That's why I posted all of my code because when I just posted an example I didn't connect it to how I was using it. I was already accessing the length in the object by String.length. I didn't want to have to go through an object to just use length though. That's where the confusion came in for me.

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by laserlight View Post
    You could do that, but an alternative is to implement an member function operator+=, then implement a free function operator+ in terms of it. An untested example:
    Code:
    String& String::operator+=(const String& rhs) {
        int templength = length + rhs.length;
        char* tempbuf = new char[templength + 1]; // Account for null terminator.
        tempbuf[0] = '\0';
        strncat(tempbuf, buf, length);
        strncat(tempbuf, rhs.buf, rhs.length);
        delete[] buf;
        length = templength;
        buf = tempbuf;
        return *this;
    }
    Now you can just write:
    Code:
    String operator+(const String& lhs, const String& rhs) {
        return String(lhs) += rhs;
    }
    Note that operator+ no longer needs to be a friend function.
    Wow I already have a overloaded += operator that looks almost identical to that. Wierd. Just out of curiosity what would be more efficient: having the + operator access the += operator or have the + operator just have all the code itself?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Just out of curiosity what would be more efficient: having the + operator access the += operator or have the + operator just have all the code itself?
    Implementing operator+ in terms of operator+= would make for easier maintenance, since you only need to fix one set of bugs. Concerning efficiency, I think that there is little difference as they are pretty much doing the same thing. Sure, there is an extra function call, but then there is also return value optimisation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Correctness, readability and maintainability first, efficiency second. Implementing + through += means less code duplication means less chance of error, less changes if something isn't right, less code to understand. All of these are Good Things that far outweigh the performance hit you might take (not guaranteed at all) from the function call in 99.5% of the cases.
    Also, by implementing + in terms of +=, you no longer need to make it a friend either, which is also a good thing as it improves encapsulation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend Functions
    By rculley1970 in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2007, 03:25 AM
  2. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  3. Friend template functions
    By lyx in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 01:11 PM
  4. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 11:37 AM
  5. Visual C++ and friend functions giving errors
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2001, 05:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21