派生类无法访问基类保护成员? [英] derived class can not access base class protected member?

查看:115
本文介绍了派生类无法访问基类保护成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到了一个奇怪的问题,派生类函数无法访问基础

类'的受保护成员。你知道为什么吗?


这是错误信息和代码。

Hello everyone,
I met with a strange issue that derived class function can not access base
class''s protected member. Do you know why?

Here is the error message and code.

展开 | 选择 | 换行 | 行号

推荐答案

>我遇到了一个奇怪的问题,即派生类函数无法访问基础
>I met with a strange issue that derived class function can not access base

> class''s受保护的成员你知道为什么吗?
>class''s protected member. Do you know why?



George,


你所拥有的代码并没有使用b作为基类,它只是和

相同虽然课程是无关的。


如果你添加:


朋友类派生;


到班级基础,然后编译,但是否这是你想要的b $ b真正想要的是另一个问题。


Dave

George,

The code you have isn''t using b as a base class, it''s just the same as
though the classes were unrelated.

If you add:

friend class derived;

to class base, it will then compile, but whether that''s what you
really want is another question.

Dave


嗨Dave,

你是什么意思
Hi Dave,
What do you mean

你所拥有的代码并没有使用b作为基类,它与

相同,尽管这些类是无关的。
The code you have isn''t using b as a base class, it''s just the same as
though the classes were unrelated.



我想我用的是代码


base * b = new derived;

delete b; //这行中的错误

函数goo中的
,派生类是对吗?

问候,

George


" David Lowndes"写道:

I think I use the code

base* b = new derived;
delete b; // error in this line

in function goo, which is in derived class right?
regards,
George

"David Lowndes" wrote:


我遇到一个奇怪的问题,派生类函数无法访问基础

class'受保护的成员。你知道为什么吗?
I met with a strange issue that derived class function can not access base
class''s protected member. Do you know why?



George,


你所拥有的代码并没有使用b作为基类,它只是和

相同虽然课程是无关的。


如果你添加:


朋友类派生;


到班级基础,然后编译,但是否这是你想要的b $ b真正想要的是另一个问题。


Dave


George,

The code you have isn''t using b as a base class, it''s just the same as
though the classes were unrelated.

If you add:

friend class derived;

to class base, it will then compile, but whether that''s what you
really want is another question.

Dave


>我想我使用代码
>I think I use the code

>
base * b = new derived;
删除b; //这行中的错误

在函数goo中,哪个派生类正确?
>
base* b = new derived;
delete b; // error in this line

in function goo, which is in derived class right?



该类是派生类,但你的用法不是。


我不知道是什么你真的想做,但因为衍生了

派生自base它已经是一个基类,没有必要创建一个
.


Dave

The class is a derived class, but your usage isn''t.

I''m not sure what you''re really trying to do, but since "derived" is
derived from "base" it already is a base class, there''s no need to
create one.

Dave


这篇关于派生类无法访问基类保护成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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