friendship from derived class method to base class members

Patrik

I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:

class Derived;
class Base
{
    int i, j;
    friend void Derived::f();
protected:
    Base();
};

class Derived : public Base
{
public:
    void f();
};

The errors I got were:

error: C2027: use of undefined type 'Derived'
see declaration of 'Derived'
error: C2248: 'Base::i' : cannot access private member declared in class 'Base'
see declaration of 'Base::i'
see declaration of 'Base'
error: C2248: 'Base::j' : cannot access private member declared in class 'Base'
see declaration of 'Base::j'
see declaration of 'Base'
error: C2027: use of undefined type 'Derived'
see declaration of 'Derived'

I struggled with it during all the day. Everything I found about friendship use only separated classes, not inheritance.

quantdev

No there is no direct way : Base needs the definition of Derived::f while Derived also needs the definition of it's Base class.

But it does not matter, you should not do that, you can, in order of preference :

  • Provide protected accessors in the Base class
  • Make the entire Derived class a friend (not necessary in general)
  • You can use an intermediate helper class which only forward the call of this specific method, and give it friendship :

Example here:

class Base;
class Derived;

class Helper final
{
    friend class Derived;

    public:
        void f(Base* base);
    private:
        Helper() {}
};

class Base
{
    int i, j;
    friend class Helper;
protected:
    Base() {}
};

class Derived : public Base
{
public:
    void f();
private:
    Helper helper;
};

void Helper::f(Base* base)
{
    base->i = 10; base->j = 5;
    std::cout << "Help !" ;
}

void Derived::f()
{
    helper.f(this);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Clone derived class from base class method

From Dev

Base class calls method from derived class?

From Dev

Clone derived class from base class method

From Dev

Why `this` can't access the derived class members from base class methods when called for derived class object

From Dev

Can static_cast be done from base class to derived class if derived class contains additional methods and members?

From Dev

Method called in creator is called from base class but not from derived class

From Dev

How does friend class of the base class access members of that base class through objects of class derived from the base class?

From Dev

C++ linking base class members to derived class members

From Dev

Derived class not inheriting overloaded method from base class

From Dev

Calling derived class method from base class pointer

From Dev

Call derived class method from base class instance

From Dev

How to call derived class method from base class pointer?

From Dev

Access to base class method only from the derived class

From Dev

Calling derived class method from base class destructor

From Dev

Method for any class derived from a generic base class

From Dev

Calling derived class method from base class destructor

From Dev

Call derived class method from base class instance

From Dev

Same data members in base and derived class

From Dev

Setting base members in derived class constructor

From Dev

Using Derived Class In a Base Method

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Method in base class that returns derived class type?

From Dev

Derived class does not call base class method

From Dev

Templated class members in the base class, don't exist in the derived classes

From Dev

Templated class members in the base class, don't exist in the derived classes

From Dev

Does an instantiation of a derived class allocate memory for private members of base class?

From Dev

Can protected base class members be initialized in a derived class via

From Dev

Is there a way to declare members of an abstract base class that are the type of the derived class?

Related Related

  1. 1

    Clone derived class from base class method

  2. 2

    Base class calls method from derived class?

  3. 3

    Clone derived class from base class method

  4. 4

    Why `this` can't access the derived class members from base class methods when called for derived class object

  5. 5

    Can static_cast be done from base class to derived class if derived class contains additional methods and members?

  6. 6

    Method called in creator is called from base class but not from derived class

  7. 7

    How does friend class of the base class access members of that base class through objects of class derived from the base class?

  8. 8

    C++ linking base class members to derived class members

  9. 9

    Derived class not inheriting overloaded method from base class

  10. 10

    Calling derived class method from base class pointer

  11. 11

    Call derived class method from base class instance

  12. 12

    How to call derived class method from base class pointer?

  13. 13

    Access to base class method only from the derived class

  14. 14

    Calling derived class method from base class destructor

  15. 15

    Method for any class derived from a generic base class

  16. 16

    Calling derived class method from base class destructor

  17. 17

    Call derived class method from base class instance

  18. 18

    Same data members in base and derived class

  19. 19

    Setting base members in derived class constructor

  20. 20

    Using Derived Class In a Base Method

  21. 21

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  22. 22

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  23. 23

    Method in base class that returns derived class type?

  24. 24

    Derived class does not call base class method

  25. 25

    Templated class members in the base class, don't exist in the derived classes

  26. 26

    Templated class members in the base class, don't exist in the derived classes

  27. 27

    Does an instantiation of a derived class allocate memory for private members of base class?

  28. 28

    Can protected base class members be initialized in a derived class via

  29. 29

    Is there a way to declare members of an abstract base class that are the type of the derived class?

HotTag

Archive