简体   繁体   中英

How to use friend function or friend class?

#include <iostream>
using namespace std;

class CBase
{   
    public:
    int a;
    int b;
    private:
    int c;
    int d;
    protected:
    int e;
    int f;
    //friend int cout1();
 };

class CDerived : public CBase
{
    public:
    friend class CBase;
    int cout1()
    {
        cout << "a" << endl;
        cin >> a;
        cout << "b" << endl;
        cin >> b;
        cout << "c" << endl;
        cin >> c;
        cout << "d" << endl;
        cin >> d;
        cout << "e" << endl;
        cin >> e;
        cout << "f" << endl;
        cin >> f;
        cout << a << "" << b << "" << c << "" << d << "" << e << "" << f << "" << endl;
    }
};

int main()
{
    CDerived chi;
    chi.cout1();
}

How to use friend class and friend function? Please help me out. I have many similar errors:

c6.cpp: In member function int CDerived::cout1():
c6.cpp:10: error: int CBase::c is private
c6.cpp:30: error: within this context
  c6.cpp:11: error: int CBase::d is private
c6.cpp:32: error: within this context
  c6.cpp:10: error: int CBase::c is private
c6.cpp:37: error: within this context
  c6.cpp:11: error: int CBase::d is private
c6.cpp:37: error: within this context

CDerived cannot access the private members of CBase . It doesn't matter if you make it a friend or not. If you want such access to be allowed, you have to make CBase declare the friendship relationship.

#include <iostream>
  using namespace std;

  class CDerived; // Forward declaration of CDerived so that CBase knows that CDerived exists

  class CBase
  {   
  public:
      int a;
      int b;
  private:
      int c;
      int d;
  protected:
      int e;
      int f;

      friend class CDerived; // This is where CBase gives permission to CDerived to access all it's members
 };

 class CDerived : public CBase
 {

 public:
 void cout1()
 {
        cout<<"a"<<endl;
        cin>>a;
        cout<<"b"<<endl;
        cin>>b;
        cout<<"c"<<endl;
        cin>>c;
        cout<<"d"<<endl;
        cin>>d;
        cout<<"e"<<endl;
        cin>>e;
        cout<<"f"<<endl;
        cin>>f;
        cout<<a<<""<<b<<""<<c<<""<<d<<""<<e<<""<<f<<""<<endl;
  } 
};

int main()
{
    CDerived chi;
    chi.cout1();
}

The best solution for you is to make the fields you access from the base class protected, not private. Still if you want to use friend , you have to make CDerived a friend class to the class CBase .

When you say

class CDerived : public CBase
 {

    public:
    friend class CBase;

That means that CBase has access to CDerived 's private members, not the other way around. Depending on your design, perhaps it is better to make those members protected . Else you need to declare CDerived as a friend of CBase .

When You're writing

friend class CBase;

it means that CBase can access private methods of CDerived. What You want to do here is to write

friend class CDerived

in CBase, so CDerived could use private methods of CBase

如果要让类B绕过类A的封装,则A必须将B声明为friend ,而不是相反。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM