Declaring function signatures of friend functions

Functions that are friend to different classes have to be declared within the classes themselves. This poses the same problem as using "magic literals" instead of variables.

1. I have to maintain each and every signatures I declared in the classes.
2. If one of the friend function declaration is not updated properly the project won't compile.

My question is how one can avoid this error-prone, tedious work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  class A {
  ///A.cc
public:
  friend T fn(){}
}

class B {
  ///B.cc
public:
  friend T fn(){}
}

class C {
  ///C.cc
public:
  friend T fn(){}
}

//other.cc
T fn() {}
> My question is how one can avoid this error-prone, tedious work?

Long-distance friendship does not work well; it makes the code less maintainable.

Error-prone, tedious friend declarations are the least of the problems. Changing the implementation of a class would instantly break the code in other components. Locating and fixing errors (say, a class invariant is violated) that crop up during testing, debugging, or in production would be a major headache.

Avoid cross-component friendship.
Topic archived. No new replies allowed.