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 ...

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?

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.

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.

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.

>> 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.

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.

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?

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.

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&#37; 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.