functions vs function objects

Can anyone explain,seeing that their implementation is quite similar when should we use function objects and when friend functions?
You use function object, functors, when you need to maintain state between calls.
Friend's introduce tight coupling; prefer a function objects to friend functions whenever possible.
Disagree: tight coupling is part of the class structure. It is not a breakage of encapsulation, but a way to enforce it. Prefer function objects when you are not duplicating state.
My motto is to stay away from friend functions. I'll leave that to other good programmers developing huge API's.
My motto is to stay away from friend functions. I'll leave that to other good programmers developing huge API's.


Agree but when we overload the ostream operator to help us print out class data member out, it is almost customary to use a friend function correct ?

1
2
3
4
5
class ABC {
  public:
  
  friend ostream& operator<<(ostream& os, const ABC& obj);
}


If you are using other approach please share as currently I use friend function whenever I want to overload the ostream operator for my classes.
It depends on the class: how much does the insertion operator need to know to serialize your class?

Besides granting friendship, it is also common to have a "to string" type of member function, which makes it unnecessary to require friendship, even though it is perfectly OK to do it (since serialization is part of a class's structure).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class ABC {
  public:
    std::string tostring() const;
};

template <typename Char, typename Traits>
inline
std::basic_ostream <Char, Traits> &
operator << (
  std::basic_ostream <Char, Traits> & outs,
  const ABC& abc
  ) {
  return outs << abc.tostring();
  }

There is nothing wrong with friend functions when it makes sense to use them. They have some very nice, low impact properties.
I always make a ToString member function to return a string representation of my class just like other libraries do (also Java/C#). But I think having friends with ostream is ok because it is so common. Almost every C++ coder out there knows about it right?

[edit]
damn, 1 minute late. Maybe some crappy net connection.
Last edited on
it is also common to have a "to string" type of member function


Any idea who as in which programming language came out with this "to string" concept ? Java has it so I was thinking it they may have borrowed the concept from C++ instead.
closed account (N6A4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class ABC {
  public:
    std::string tostring() const;
};

template <typename Char, typename Traits>
inline
std::basic_ostream <Char, Traits> &
operator << (
  std::basic_ostream <Char, Traits> & outs,
  const ABC& abc
  ) {
  return outs << abc.tostring();
  }


Okay, that is just confusing.
I/O is messy. And confusing if you don't know much about it. A simpler version (that only works on byte streams) is:

1
2
3
4
ostream& operator << ( ostream& outs, const ABC& abc )
  {
  return outs << abc.tostring();
  }
closed account (N6A4jE8b)
Uh...What are byte streams?

Sorry, I guess I should go back to the beginner's section pronto.
western wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
10
11
12
13
14	class ABC {
  public:
    std::string tostring() const;
};

template <typename Char, typename Traits>
inline
std::basic_ostream <Char, Traits> &
operator << (
  std::basic_ostream <Char, Traits> & outs,
  const ABC& abc
  ) {
  return outs << abc.tostring();
  }



Okay, that is just confusing.

Look here http://cplusplus.com/doc/tutorial/templates/ and here http://cplusplus.com/reference/iostream/

western wrote:
Uh...What are byte streams?

Sorry, I guess I should go back to the beginner's section pronto.

Look here, http://en.wikipedia.org/wiki/Stream_(computing) and you can think of a byte as a character but uhmm, not always.
Topic archived. No new replies allowed.