这是正确的吗?对于另一个对象,不能从derivedclass方法调用基类的受保护成员 [英] Is this right? Can't call protected member of base class from derivedclass method, for another object

查看:50
本文介绍了这是正确的吗?对于另一个对象,不能从derivedclass方法调用基类的受保护成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上这个:


//文件''B.hh''

B级

{

受保护:

void f(){}

};


//文件''C。 hh''

#include" B.hh"

class C

{

public:

void doit(B& arg)

{

//做一些事情

arg.f(); < br $> b $ b}

};


显然我不能这样做(根据GCC 3.3.1)。关于如何实现这一点的任何提示,考虑到我不想做以下事情?


C级;

B级

{

朋友C级;

....

谢谢,

Asfand Yar

-
http://www.it-is-truth.org/

解决方案

>

显然我不能这样做(根据GCC 3.3.1)。关于如何实现这一点的任何提示,考虑到我不想做类似以下的事情?




那么,为什么f受到保护?通过保护它,那个人不应该做你想要做的事情。没有任何意义。


无论如何:

B级

{

受保护:
void f(){}

public:

void g(){f();}

}


在C中拨打g

-

Gabriel


< blockquote> Asfand Yar Qazi< im_not_giving_it_here@i_hate_spam.com>在消息新闻中写道:< bj ********** @ newsg4.svr.pol.co.uk> ...

基本上这个:
//文件''B.hh''
B级
{
受保护:
void f(){}
};

//文件''C.hh''
#include" B.hh"
C类
{
公开:
void doit(B& arg)
//做一些东西
arg.f();
}
};

显然我可以''这样做(根据GCC 3.3.1)。关于如何实现这一点的任何提示,考虑到我不想做以下事情?


如果你保持

f()受保护,没有朋友限定符就无法做到这一点。受保护的成员只能通过其类或其类后代的方法访问。


使f()成为公共成员并且doit(B& arg)将能够访问它。

C类;

B类
{
朋友类C;
...

谢谢,
Asfand Yar




Marcelo Pinto




" Asfand Yar Qazi" < im_not_giving_it_here@i_hate_spam.com>在消息中写道

news:bj ********** @ newsg4.svr.pol.co.uk ...

基本上这个:

//档案''B.hh''
B班
{
受保护:
void f(){}
};

//档案''C.hh''
#include" B.hh"
C班
{
公开:
void doit(B& arg)
{
//做一些东西
arg.f();
}
};

显然我不能这样做(根据GCC 3.3.1)。


不,显然不是。这就是保护东西的全部目标。

谁设计了B?如果是你,那么你的设计就会有一些不太合适的东西。

关于如何实现这一点的任何提示,考虑到我不这样做想做下面的
之类的事情?
C班;

B班
{
朋友班C;




你可以只把这个功能变成朋友。但同样,为什么你想要打破设计?


Basically this:

//file ''B.hh''
class B
{
protected:
void f() {}
};

//file ''C.hh''
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can''t do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?

class C;

class B
{
friend class C;
....
Thanks,
Asfand Yar
--
http://www.it-is-truth.org/

解决方案

>

Apparently I can''t do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?



So, why is f protected? By making it protected, that one should not do
what you are trying to do. Makes no sense.

Anyway:
class B
{
protected:
void f() {}
public:
void g() {f();}
}

call g in C

--
Gabriel


Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> wrote in message news:<bj**********@newsg4.svr.pol.co.uk>...

Basically this:

//file ''B.hh''
class B
{
protected:
void f() {}
};

//file ''C.hh''
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can''t do this (according to GCC 3.3.1). Any tips on how to
achieve this, considering I do not want to do something like the following?
There is no way of doing it without the friend qualifier if you keep
f() protected. Protected members can be accessed only in methods of
its class or its class'' descendants.

Make f() a public member and doit(B& arg) will be able to access it.

class C;

class B
{
friend class C;
...
Thanks,
Asfand Yar



Marcelo Pinto



"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
news:bj**********@newsg4.svr.pol.co.uk...

Basically this:

//file ''B.hh''
class B
{
protected:
void f() {}
};

//file ''C.hh''
#include "B.hh"
class C
{
public:
void doit(B& arg)
{
// do some stuff
arg.f();
}
};

Apparently I can''t do this (according to GCC 3.3.1).
No, obviously not. That''s the whole poinf of making something protected.
Who designed B? If it was you, then there''s something not quite right with
your design.
Any tips on how to
achieve this, considering I do not want to do something like the following?
class C;

class B
{
friend class C;



You can make only the function a friend. But again, why do you want to
break the design?


这篇关于这是正确的吗?对于另一个对象,不能从derivedclass方法调用基类的受保护成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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