Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?
Asked Answered
R

2

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?

Retaliate answered 24/11, 2017 at 9:4 Comment(0)
O
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; }
Outshine answered 24/11, 2017 at 9:8 Comment(7)
It doesn't slice, friendFuncReturn takes a reference. Slicing is when you copy into a base from a derived objectYip
@Yip 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);).Outshine
A derivedClass object bound to a baseClass reference is still the same object, it is not sliced. You can static_cast<derivedClass&> it back. The function has to be the same for all possible baseClass subtypes, because there's only one of it.Yip
And specifically baseClass & is a reference, not a value. There is no copyingYip
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.Outshine
int friendFuncReturn(baseClass obj) <- recieved by value, will slice derived objects. obj is only a baseClass, it isn't a derivedClassYip
@Yip 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).Outshine
N
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.

Nerine answered 24/11, 2017 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.