课堂上的朋友无法访问私人功能 [英] Friend in Class Can't Access Private Function

查看:52
本文介绍了课堂上的朋友无法访问私人功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMain Class是在main函数中初始化的基类。 CA

Class是在CMain :: CMain()中初始化的基类。 CMain类

始终是公共的,而CA Class始终是私有的。我已经放置了朋友

void CA :: Run_A(void)在CMain类。 CMain :: Run()函数尝试执行CA :: Run_A()
,但编译器显示错误,说明访问私有函数是违反

的行为。

我不明白为什么因为朋友应该能够访问私人

功能。请指教。


/ * Ah * /

等级CMain;


等级CA

{

public:

CA(CMain * pMain);

~CA();


私人:

CA();

无效Run_A(无效);

CMain * m_pMain;

};


/ * A.cpp * /

#include" A.h"


CA :: CA()

{


}


CA :: CA(CMain * pMain)

{

m_pMain = pMain;

}


CA :: ~CA()< br $>
{


}


无效CA :: Run_A(无效)

{


}


#include< stdio.h>

#include" A.h"


类CMain

{

公开:

CMain();

~CMain();

void Run(void);

friend void CA :: Run_A(void);

$ b $私人:

CA * m_pA;

};


CMain :: CMain()

{

m_pA =新CA(本);

}


CMain ::〜CMain()

{

删除m_pA;

}


无效CMain ::运行(无效)

{

CA :: Run_A();

}


void main(无效)

{

CMain Main;

}

-

Bryan Parkoff

解决方案

2004年4月12日星期一01:26:08 GMT,Bryan Parkoff

< br *********** *******@nospam.com>写道:

CMain Class是在main函数中初始化的基类。 CA
Class是在CMain :: CMain()中初始化的基类。 CMain类
始终是公共的,而CA Class始终是私有的。我已经放置了朋友
void CA :: Run_A(void)在CMain类。 CMain :: Run()函数尝试执行CA :: Run_A(),但编译器显示错误,说明访问私有函数是违反
的。
我不是明白为什么因为朋友应该能够访问私人功能。请指教。


你有两个问题。首先,您的CMain课程试图在CA中声明该职能的友情。这不是它的工作原理。这个类需要

你需要访问必须/授予/友谊的类需要

的访问权限。所以你把类似

的朋友类CMain;

放到CA的类定义中(或者你可以将它限制为特定的函数,如

你已经证明你有一般性的想法。)


另一件事是你正在调用一个成员函数,好像它是一个

静态函数。见下文......

/ *啊* /
班级CMain;

班级CA
{
公开:
CA(CMain * pMain);
~CA();

私人:
CA();
void Run_A(void);
CMain * m_pMain;
};

/ * A.cpp * /
#include" A.h"

CA :: CA()<


CA :: CA(CMain * pMain)
{
m_pMain = pMain;
}

CA :: ~CA()
{

}

无效CA :: Run_A(无效)
{

}

< stdio.h>
#include" A.h"

class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA :: Run_A(void);

私人:
CA * m_pA;
};

CMain :: CMain()
{
m_pA =新CA(这个);
}

CMain ::〜CMain()
{
删除m_pA;
}

无效CMain: :运行(无效)
{
CA :: Run_A();


以上,你可以这样做:

m_pA - > Run_A();

(你的课程结交了朋友之后)但你不能调用会员函数

没有一个对象来调用它。

-leor

void main(无效)
{CMain Main;
}




-

Leor Zolman --- BD软件--- www。 bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误信息Decryptor at:
www.bdsoft.com/tools/stlfilt.html


2004年4月12日星期一01:47:58 GMT,Leor Zolman< le ** @ bdsoft.com>写道:

(你的课程结交了朋友之后)但你不能调用成员函数
而没有一个对象来调用它。




我累了。我所说的两个地方是会员功能,我真正想要的是非静态的。会员功能。

-leor


-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl的现场培训和Unix

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


Leor,


谢谢你的回答。如果我放置朋友类CMain在CA类中,

所有函数和数据成员都将被CMain类访问。我不会允许b $ b允许发生。允许只有一个私人函数的另一种方式是作为朋友访问
?我不明白你的意思是静态还是非静态

功能就像课外一样。请解释。


-

Bryan Parkoff

" Leor Zolman" <乐** @ bdsoft.com>在留言中写道

news:v2 ******************************** @ 4ax.com ...

2004年4月12日星期一01:26:08 GMT,Bryan Parkoff
< br ************** ****@nospam.com>写道:

CMain Class是在main函数中初始化的基类。
CAClass是在CMain :: CMain()中初始化的基类。 CMain
Classis始终公开,而CA Class始终是私有的。我已经放置了朋友
void CA :: Run_A(void)在CMain类。 CMain :: Run()函数尝试执行CA :: Run_A(),但编译器显示错误,说明访问私有函数是违反
的。
我不是明白为什么因为朋友应该能够访问
私函数。请指教。
你有两个问题。首先,您的CMain课程试图在CA中声明该职能的友谊。这不是它的工作原理。你需要访问的课程必须/授予/友谊到需要访问权限的班级。所以你把像朋友类CMain;
这样的东西放到CA的课堂定义中(或者你可以将它限制在特定的功能中,因为你已经证明你有一般的想法。)

另一件事是你正在调用一个成员函数,好像它是一个静态函数。见下文......


/ *啊* /
班级CMain;

班级CA
{
public:
CA(CMain * pMain);
~CA();

私人:
CA();
void Run_A(void );;
CMain * m_pMain;
};

/ * A.cpp * /
#include" A.h"
CA :: CA()
{

}

CA :: CA(CMain * pMain)
{
m_pMain = pMain ;


CA ::〜CA()
{

}

void CA :: Run_A(void )
{

}
#include< stdio.h>
#include" A.h"
班级CMain
{
公开:
CMain();
~CMain();
void Run(void);
朋友无效CA :: Run_A(无效);

私人:
CA * m_pA;
};

CMain :: CMain()
{
m_pA =新CA(这);


CMain ::〜CMain()
{
删除m_pA;
}
void CMain :: Run(无效)
{
CA :: Run_A();



上面,您可以执行以下操作:
m_pA - > Run_A();
(在你的班级交了朋友之后),但你不能叫一个成员



函数而没有一个对象来调用它。
-leor


void main(void)
{CMain Main;
}



-
Leor Zolman --- BD软件--- www.bdsoft.com
C / C ++,Java,Perl和Unix的现场培训
C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html



CMain Class is the base class that is initialized in main function. CA
Class is the base class that is initialized in CMain::CMain(). CMain Class
is always public while CA Class is always private. I have placed "friend
void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don''t understand why because friend should be able to access private
function. Please advise.

/* A.h */
class CMain;

class CA
{
public:
CA(CMain* pMain);
~CA();

private:
CA();
void Run_A(void);
CMain* m_pMain;
};

/* A.cpp */
#include "A.h"

CA::CA()
{

}

CA::CA(CMain* pMain)
{
m_pMain = pMain;
}

CA::~CA()
{

}

void CA::Run_A(void)
{

}

#include <stdio.h>
#include "A.h"

class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void);

private:
CA* m_pA;
};

CMain::CMain()
{
m_pA = new CA(this);
}

CMain::~CMain()
{
delete m_pA;
}

void CMain::Run(void)
{
CA::Run_A();
}

void main(void)
{
CMain Main;
}
--
Bryan Parkoff

解决方案

On Mon, 12 Apr 2004 01:26:08 GMT, "Bryan Parkoff"
<br******************@nospam.com> wrote:

CMain Class is the base class that is initialized in main function. CA
Class is the base class that is initialized in CMain::CMain(). CMain Class
is always public while CA Class is always private. I have placed "friend
void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don''t understand why because friend should be able to access private
function. Please advise.
You have two problems. Firstly, your CMain class is trying to "claim"
friendship of that function in CA. That''s not how it works. The class with
the thing you need access to must /grant/ friendship to the class needing
the access. So you put something like
friend class CMain;
into CA''s class definition (or you can limit it to specific functions, as
you''ve shown you have the general idea about.)

The other thing is that you''re calling a member function as if it were a
static function. See below...

/* A.h */
class CMain;

class CA
{
public:
CA(CMain* pMain);
~CA();

private:
CA();
void Run_A(void);
CMain* m_pMain;
};

/* A.cpp */
#include "A.h"

CA::CA()
{

}

CA::CA(CMain* pMain)
{
m_pMain = pMain;
}

CA::~CA()
{

}

void CA::Run_A(void)
{

}

#include <stdio.h>
#include "A.h"

class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void);

private:
CA* m_pA;
};

CMain::CMain()
{
m_pA = new CA(this);
}

CMain::~CMain()
{
delete m_pA;
}

void CMain::Run(void)
{
CA::Run_A();
Above, you can do something like:
m_pA -> Run_A();
(after your classes have made friends) but you can''t call a member function
without an object to invoke it on.
-leor

void main(void)
{
CMain Main;
}



--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


On Mon, 12 Apr 2004 01:47:58 GMT, Leor Zolman <le**@bdsoft.com> wrote:

(after your classes have made friends) but you can''t call a member function
without an object to invoke it on.



I''m getting tired. The two places whre I said "member function", what I
really meant was "non-static" member function.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


Leor,

Thank you for the answer. If I place "friend class CMain" in CA class,
all functions and data members will be accessed by CMain class. I do not
allow to happen. What is another way to allow only one private function be
accessed as friend? I don''t understand what you mean static or non static
function like outside of class. Please explain.

--
Bryan Parkoff
"Leor Zolman" <le**@bdsoft.com> wrote in message
news:v2********************************@4ax.com...

On Mon, 12 Apr 2004 01:26:08 GMT, "Bryan Parkoff"
<br******************@nospam.com> wrote:

CMain Class is the base class that is initialized in main function. CAClass is the base class that is initialized in CMain::CMain(). CMain Classis always public while CA Class is always private. I have placed "friend
void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don''t understand why because friend should be able to access privatefunction. Please advise.
You have two problems. Firstly, your CMain class is trying to "claim"
friendship of that function in CA. That''s not how it works. The class with
the thing you need access to must /grant/ friendship to the class needing
the access. So you put something like
friend class CMain;
into CA''s class definition (or you can limit it to specific functions, as
you''ve shown you have the general idea about.)

The other thing is that you''re calling a member function as if it were a
static function. See below...


/* A.h */
class CMain;

class CA
{
public:
CA(CMain* pMain);
~CA();

private:
CA();
void Run_A(void);
CMain* m_pMain;
};

/* A.cpp */
#include "A.h"

CA::CA()
{

}

CA::CA(CMain* pMain)
{
m_pMain = pMain;
}

CA::~CA()
{

}

void CA::Run_A(void)
{

}

#include <stdio.h>
#include "A.h"

class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void);

private:
CA* m_pA;
};

CMain::CMain()
{
m_pA = new CA(this);
}

CMain::~CMain()
{
delete m_pA;
}

void CMain::Run(void)
{
CA::Run_A();



Above, you can do something like:
m_pA -> Run_A();
(after your classes have made friends) but you can''t call a member


function without an object to invoke it on.
-leor


void main(void)
{
CMain Main;
}



--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html



这篇关于课堂上的朋友无法访问私人功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆