4
class baseClass {
public:
  friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; }
  baseClass(int x) : baseInt(x) {}
private:
  int baseInt;
};

class derivedClass : public baseClass {
public:
  derivedClass(int x, int y) : baseClass(x), derivedInt(y) {}
private:
  int derivedInt;
};

in the function friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; } I don't understand why would the friend function of the base class work for the derived class? should not passing derived class obj. instead of a base class obj. tconsidered as error? my question is why it works when i pass a derived class object to it?

2 Answers2

5

Are friend functions inherited?

No, friend functions are not inherited.

Why would a base class function work on a derived class object?

Because friend function is using the data members available in base class only. Not the data members of derived class. Since derived class is a type of base class So, friend function is working fine. But note that here derived class instance is sliced and having information available only for base class. friend function will report an error if you will try to access restricted members of derived class. e.g.

 int friendFuncReturn(baseClass &obj) { return ((derivedClass)obj).derivedInt; }
cse
  • 4,066
  • 2
  • 20
  • 37
  • It doesn't slice, `friendFuncReturn` takes a reference. Slicing is when you *copy* into a base from a derived object – Caleth Nov 24 '17 at 11:15
  • @Caleth It is the same here i.e. copying into a base class object(function parameter `baseClass &obj`) from a derived object(passed argument e.g. `derivedClass d; friendFuncReturn(d);`). – cse Nov 24 '17 at 11:37
  • A `derivedClass` object bound to a `baseClass` reference is still the same object, it is not sliced. You can `static_cast` it back. The function has to be the same for all possible `baseClass` subtypes, because there's only one of it. – Caleth Nov 24 '17 at 11:41
  • And specifically `baseClass &` is a reference, not a value. There is no copying – Caleth Nov 24 '17 at 11:45
  • Can you provide any example in which I'm passing the instance of `derived` class to the `base` class and can't get the value of `derived` class instance back? Also please give me an example of `object slicing`. – cse Nov 24 '17 at 11:47
  • `int friendFuncReturn(baseClass obj)` <- recieved by value, will slice derived objects. `obj` is only a `baseClass`, it isn't a `derivedClass` – Caleth Nov 24 '17 at 11:48
  • @Caleth Yes, But since we are _Putting_ an instance of `derived class` in `base class` variable, So it is `object slicing` i.e. Information of `derived class` are not available in `base class` variable and hence we cannot access of `derived class` feature here(which is `object slicing`). – cse Nov 24 '17 at 12:09
3

No. You cannot inherited friend function in C++. It is strictly one-one relationship between two classes.

C++ Standard, section 11.4/8

Friendship is neither inherited nor transitive.

msc
  • 33,420
  • 29
  • 119
  • 214