【问题标题】:Access friend function访问好友功能
【发布时间】:2013-10-27 14:35:46
【问题描述】:

在从 main 访问 Class_D 中声明的函数朋友时需要帮助。 继续进行的指导。

    /* Main.cpp */

    #include <iostream>
    #include "types.h"
    #include "Class_A.h"
    #include "Class_C.h"

    int main()
    {
     cout << " Project started" << endl; 
     /* Creating Obj of Class A */
     A obj1; 
     /* Accessing Funcition of Class C through A */
     obj1.SetFuncA();         
     /* How to access GetFuncD(); from main*/
     cin.get();   
     return 0;   
    }

/* types.h */
#ifndef TYPES_H
#define TYPES_H

#include <iostream>
#include <stdlib.h>

#define ENAB_FRIEND_CLASS(x) friend class x

using namespace std;

typedef unsigned int U32;
typedef unsigned short  U16;
typedef unsigned char U8;


typedef int S32;
typedef short S16;
typedef char S8;


#endif

/* Class_A.h*/

#ifndef CLASS_A_H
#define CLASS_A_H


class D;

class A {
      private :
         int i;
         int j;     
      public :                  
         A();           /* Default Constructor */                                                                  
         ~A();          /* Default Destructor */                                                                                              
         void SetFuncA();
         int GetFuncA();
         friend int D::FGetFuncD(D &obj);
      protected:            
        };

#endif

/* Class_D.h */
#ifndef CLASS_D_H
#define CLASS_D_H

class D {
      private :
         int i;
         int j;     
      public :                  
         D();           /* Default Constructor */                                                                  
         ~D();          /* Default Destructor */                                                                                              
         void SetFuncD();
         int GetFuncD();
         void FGetFuncD(D &obj);
      protected:            
        };

void FGetFuncD(D &obj)
{
cout << "\n i " << obj.i << endl;     
cout << "\n i " << obj.j << endl;
}


#endif

/* Class_A.cpp */


#include "Class_A.h"
#include "types.h"
#include "Class_C.h"

A :: A()
{
cout << "Default CTOR Called\n" << endl;     
}

A :: ~A()
{
cout << "Default DTOR Called\n" << endl;     
}

void A::SetFuncA()
{
  int ret = 0;   
  cout << "\n SetFuncA " << endl;

  /* Creating Object of class C in Class A*/
  C Obj2;   

  /* Setting Private members */
  Obj2.SetFuncC();

  /* Calling Function of class C in Class A */
  ret = Obj2.GetFuncC();

  cout << " M = " << ret << endl;

  /* Dynamically Allocate memory for Class C */
  C *ptr = new C();

  /* Accessing private members of Class C */
  ptr->m =20;

  /* Accessing Public Function of Class C*/
  ret = ptr->GetFuncC();

  cout << " M = " << ret << endl;   

  /* Accessing Enum */
  ptr->m_bLEVEL = ptr->KN_LEVEL_1;

  cout << " ENUM = " << ptr->m_bLEVEL << endl;



}


int A::GetFuncA()
{
    cout << "\n GetFuncA " << endl;   
}


/* Class_D.cpp*/

#include "types.h"
#include "Class_D.h"

D :: D()
{
cout << "Default CTOR Called\n" << endl;     
}

D :: ~D()
{
cout << "Default DTOR Called\n" << endl;     
}

void D::SetFuncD()
{
  cout << "\n SetFuncD " << endl;
  i = 30;
}

int D::GetFuncD()
{
  cout << "\n GetFuncD " << endl;
  return i;
}

请指导我修改需要做的修改,以使用朋友功能访问 class_d 的私有成员。

我正在尝试探索好友功能的特性。 我添加了 Class_A.cpp/.h Class_D.cpp/.h 和 main.cpp。

【问题讨论】:

  • 你到底有什么问题?您使用 D::GetFuncD 的方式与使用 class A 的方式相同。包含class_d.h,创建一个D对象,然后为它调用GetFuncD方法。

标签: c++ private-members friend-function


【解决方案1】:

友元函数是一个不是类成员但可以访问类的私有成员和受保护成员的函数。 所以,你的 D 类应该从:

 public :                  
     D();           /* Default Constructor */                                                                  
     ~D();          /* Default Destructor */                                                                                              
     void SetFuncD();
     int GetFuncD();
     void FGetFuncD(D &obj);

到:

 public :                  
     D();           /* Default Constructor */                                                                  
     ~D();          /* Default Destructor */                                                                                              
     void SetFuncD();
     int GetFuncD();
     friend void FGetFuncD(D &obj);  /* changed to friend function */

Here's pretty good documentation for it from Microsoft.

然后,在 main 中,您可以在没有 D 的实例化对象的情况下调用 FGetFuncD。

int main()
{
 D obj2;
 obj2.SetFuncD();
 int i_of_obj2 = FGetFuncD(obj2); /*using GetFuncD WITHOUT calling on a D object*/
 cout << "i_of_obj2: " << i_of_obj2 << endl;


 cin.get();   
 return 0;   
}

输出应该是: i_of_obj2: 30

【讨论】:

  • 嗨!你对成员函数和朋友函数感到困惑。友元函数在 class_D.h 文件中声明。你提到的功能是成员功能。感谢您的关注。 void FGetFuncD(D &obj) { cout
  • 你好 mxdg !!我有一个查询..当我必须传递 d 类的对象时,朋友函数的用途是什么。当我能够创建对象时。我可以使用对象访问私有成员。我的查询不是创建类 d 的对象然后获取调用的友元函数
  • 对于任何功能,我在上面详细说明了如何使其成为朋友功能。朋友功能是为了即使它不是成员函数,您也可以访问事物。无论哪种方式,您都需要一个类的实例。例如,可以使用友元函数来比较两个 D 类对象,看看它们的 i 是否相同。 friend bool CompareI(D &obj1, D &obj2){ return (obj1.i == obj2.i) } 如果这不是友元函数,我们将无法访问 i 成员变量。这会让事情更清楚吗?
  • 嗨 mxdg !这很有帮助!
猜你喜欢
  • 2015-08-02
  • 2012-05-22
  • 2021-08-05
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多