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

Moq a base class function from a derived class

From Dev

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

From Dev

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

From Dev

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

From Dev

How to get subtype of a derived class in base class

From Dev

Can I call a derived method from 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 a derived class pointer to a base class object call methods of the derived class?

From Dev

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

From Dev

Passing derived class to base function

From Dev

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

From Dev

How can I initialize an instance of a derived class from an instance of the base 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?

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 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 have an object of a derived class "reference" members of an instance of the base class?

From Dev

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

From Dev

Base Class Function Not Calling Derived Class Function

From Dev

How can I define asp.net Htmlhelper extensions for derived classes of an abstract 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 maintain a compile-time list of all types derived from a base class?

From Dev

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

From Dev

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

From Dev

Passing derived class to base function

From Dev

Printing from a derived class with a base class function

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

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?

Related Related

  1. 1

    Moq a base class function from a derived class

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

    How to get subtype of a derived class in base class

  6. 6

    Can I call a derived method from 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 a derived class pointer to a base class object call methods of the derived class?

  9. 9

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

  10. 10

    Passing derived class to base function

  11. 11

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

  12. 12

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

  13. 13

    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?

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

    Base Class Function Not Calling Derived Class Function

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

    Passing derived class to base function

  25. 25

    Printing from a derived class with a base class function

  26. 26

    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?

  27. 27

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

  28. 28

    Can a virtual function access the friend of base class?

  29. 29

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

HotTag

Archive