2013-10-27 51 views
0

需要帮助从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。

+1

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

回答

1

friend函数是一个函数,它不是类的成员,但可以访问该类的私有和受保护成员。 所以,你的类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.

然后,在主,你可以叫FGetFuncD没有D.

的一个实例化对象
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

+0

嗨!你对成员函数和朋友函数感到困惑。朋友函数在class_D.h文件中声明。你提到的函数是成员函数。感谢您的关注。 void FGetFuncD(D&obj) { cout <<“\ n i”<< obj.i << endl; cout <<“\ n i”<< obj.j << endl; } –

+0

HI mxdg !!我有一个查询..当我必须通过d类的对象然后什么是朋友函数的使用。当我能够创建对象。我可以使用对象访问私有成员。我的查询不是创建类d的对象,然后获取名为 –

+0

的朋友函数。对于任何函数,我都详细说明了如何使它成为上面的朋友函数。好友功能是让你可以访问的东西,即使它不是一个成员函数。您需要任何一种类的实例。 例如,可以使用朋友函数来比较两个D类对象,以查看他们的i是否具有相同的值。 朋友布尔CompareI(d&OBJ1,d&OBJ2){ 回报(obj1.i == obj2.i) } 如果不是一个朋友的功能,我们将无法访问我的成员变量。这是否使事情更清楚? – mxdg