Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

Friend Functions...

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what have you used them for? i have only used them for overloading the input and output streams but wondered if i had really gained much(i.e. it was a bit tideous to get it to work the first time at least). was wondering besides using friend for I/O, what else have you used it for and why and did you really gain anything from it. from what i have done with C++ and read online some authors tell you to use it with caution, others will print a whole section on it and some will even dismiss it from their books/articles all together b/c they dont think it should be part of the language and should be avoided at all costs. was wanting to get your input.. thanks!

Share this post


Link to post
Share on other sites
Advertisement
They are kind of contentious from an OOP point of view, but I will say that, IIRC, Bjarne himself says they are ok under the right circumstances on his FAQ page. He elaborated a little, but I can't remember what he said.

The only thing I've used them for so far is symmetrical operators like *, so that, for example, you can write:

Vector3 v2 = 5.0f * v1;

Or:

Vector3 v2 = v1 * 5.0f;

OOP experts will likely give you a detailed reply on when and when not to use them.

Share this post


Link to post
Share on other sites
Non-member mixed-mode binary operators (other than IO) can be usefully made friends.
Same with functions which could be made member functions, but provide a more elegant syntax as non-members (e.g. dot(a,b) vs. a.dot(b)), or for coherence with a third-party framework.

Friend classes aren't much different from friend functions, IMHO.

Share this post


Link to post
Share on other sites
If you have a hierarchy of classes then you can make a Write(ostream& os) member function which calls a virtual function to be overriden by derived classes.


class Base {
public:
ostream& Write(ostream& os) const {
//..some stuff
return DoWrite(os);
}
protected:
virtual ostream& DoWrite(ostream& os) const {
}
};




//doesn't need to be a friend
ostream& operator<<(ostream& os, const Base& obj) {
return obj.Write(os);
}




You only have to write the non-member function operator<< once for each hierarchy. Derived class override the virtual function DoWrite.

So you don't need to make I/O functions friends either. It's just an unnecessary convenience.

Share this post


Link to post
Share on other sites
The only thing I use friends for is to allow my own classes to construct instances of another class under tightly controlled circumstances, without allowing users of the code to do the same.

Example (this isn't real code, it just demonstrates what I mean):
template <typename TYPE, int DIMENSION>
class Bounds
{

public:

friend class Bounds<TYPE, DIMENSION - 1>;

Bounds<TYPE, DIMENSION + 1> operator[](unsigned int bound) const;

private:

Bounds(const unsigned int* const lowerOrderBounds, unsigned int bound);

};

template <typename TYPE, int DIMENSION>
Bounds<TYPE, DIMENSION + 1> Bounds<TYPE, DIMENSION>::operator[](unsigned int bound) const
{
return Bounds<TYPE, DIMENSION + 1>(bounds_, bound);
}


This enables me to guarantee at compile-time that Bounds<TYPE, N> is constructed with exactly N unsigned integers.

By the way, if anybody knows of a better way to accomplish this I'd be interested to know. I looked, but didn't find anything.

Enigma

Share this post


Link to post
Share on other sites
With smart pointers, it makes more sense not to have any member functions, because it can be confusing deciphering between this:

f->memberFuncion(); //of pointee
f.memberFunction(); //of pointer

Share this post


Link to post
Share on other sites
hey jyk...thats a good way to use it....didnt think of using it that way..but would you NEED to use a friend for this? thanks all for the replys..most of the time it just would seem overkill.
makes you wonder if it is really your "friend" eh? hehe

i am going to play with the so-called "friend" some more and then i will re-post tomorrow.. thanks all for the input!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!