How can I friend a derived class function in the base class?

Collin

I have the following code:

#include <iostream>

class A {
    private:
        int a;
    public:
        void setA(int a_);
    friend int B::getA();
};

class B : public A {
    public:
        int getA();
};

void A::setA(int a_) {
    a = a_;
}

int B::getA() {
    return a;
}

int main() {
    B myB;
    myB.setA(9);

    std::cout << myB.getA()<< std::endl;
    return 0;
}

Compiling with g++ yields:

friend.cpp:10:16: error: use of undeclared identifier 'B'
    friend int B::getA();

My thinking is that when the compiler is going through the class A definition, it does not yet know about class B. Therefore, I can forward declare B to take care of this problem:

#include <iostream>

class B;

class A {
...

That doesn't quite work:

friend.cpp:10:16: error: incomplete type 'B' named in nested name specifier
    friend int B::getA();

It looks like the compiler isn't able to resolve the function as it is given.

How can I make a derived class function a friend in the base class?

Laszlo

Your code seems to violate the basic concept of data encapsulation. To resolve it, either make A::a protected, as @rici suggested, or define a getter in class A.

class A {
private:
   int a;
public:
   void setA(int a_);
   virtual int getA();
};

class B : public A {
public:
   int getA();
};

void A::setA(int a_) {
   a = a_;
}
int A::getA() {
   return a;
}

int B::getA() {
   return A::getA();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How a friend function of a base class is calling the overridden virtual function of derived class even though they are private ?(C++)

From Dev

How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

From Dev

Can a virtual function access the friend of base class?

From Dev

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

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

How can I store in a derived class information obtained during initialization of a base class?

From Dev

How can I create an instance of a derived class from an instance of a base class and include private fields?

From Dev

How can I initialize an instance of a derived class from an instance of the base class?

From Dev

How can I have an object of a derived class "reference" members of an instance of the base class?

From Dev

How can I store in a derived class information obtained during initialization of a base class?

From Dev

How to call a function from a derived class in a base class?

From Dev

Base Class Function Not Calling Derived Class Function

From Dev

How can a derived class invoke private method of base class?

From Dev

Passing derived class to base function

From Dev

Passing derived class to base function

From Dev

How to pass a derived class to a base class function and return the derived class c#

From Dev

How can a derived class pointer to a base class object call methods of the derived class?

From Dev

How can a derived class pointer to a base class object call methods of the derived class?

From Dev

Moq a base class function from a derived class

From Dev

Printing from a derived class with a base class function

From Dev

Can I call a derived method from base class?

From Dev

Can I forward template arguments of a derived class to the base in CRTP?

From Dev

How can I define asp.net Htmlhelper extensions for derived classes of an abstract base class?

From Dev

How can I maintain a compile-time list of all types derived from a base class?

From Dev

how do I specialize a bound template friend function to a template class?

From Dev

How to get subtype of a derived class in base class

From Dev

Can I alias a member of the base class in derived class without increasing class memory?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

Related Related

  1. 1

    How a friend function of a base class is calling the overridden virtual function of derived class even though they are private ?(C++)

  2. 2

    How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

  3. 3

    Can a virtual function access the friend of base class?

  4. 4

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

  5. 5

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

  6. 6

    How can I store in a derived class information obtained during initialization of a base class?

  7. 7

    How can I create an instance of a derived class from an instance of a base class and include private fields?

  8. 8

    How can I initialize an instance of a derived class from an instance of the base class?

  9. 9

    How can I have an object of a derived class "reference" members of an instance of the base class?

  10. 10

    How can I store in a derived class information obtained during initialization of a base class?

  11. 11

    How to call a function from a derived class in a base class?

  12. 12

    Base Class Function Not Calling Derived Class Function

  13. 13

    How can a derived class invoke private method of base class?

  14. 14

    Passing derived class to base function

  15. 15

    Passing derived class to base function

  16. 16

    How to pass a derived class to a base class function and return the derived class c#

  17. 17

    How can a derived class pointer to a base class object call methods of the derived class?

  18. 18

    How can a derived class pointer to a base class object call methods of the derived class?

  19. 19

    Moq a base class function from a derived class

  20. 20

    Printing from a derived class with a base class function

  21. 21

    Can I call a derived method from base class?

  22. 22

    Can I forward template arguments of a derived class to the base in CRTP?

  23. 23

    How can I define asp.net Htmlhelper extensions for derived classes of an abstract base class?

  24. 24

    How can I maintain a compile-time list of all types derived from a base class?

  25. 25

    how do I specialize a bound template friend function to a template class?

  26. 26

    How to get subtype of a derived class in base class

  27. 27

    Can I alias a member of the base class in derived class without increasing class memory?

  28. 28

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  29. 29

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

HotTag

Archive