多态性和受保护的帮助 [英] polymorphism and protected help

查看:56
本文介绍了多态性和受保护的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在理解衍生类为什么不能通过以下代码访问基类的受保护成员时遇到一些麻烦:

#include< stdio.h>


班级CBase

{

受保护:

int x;

public:

CBase(){x = 123;}

public:

operator int(){return x;}

};


class CDerived:public CBase

{

public:

CDerived()

{

CBase * p = this;

p- > x = 456; //错误:无法访问受保护的成员

}

};


void main()

{

printf(" CBase =%d \ nCDerived =%d \ n",CBase(),CDerived());

}

我不明白为什么CDerived不能访问整数x在

CBase没有让CBase宣布CDerived为朋友。可以有人

向我解释为什么会这样?谢谢。

解决方案

xxx写道:

班级CBase
{
受保护:
int x;
};

类CDerived:public CBase
{
公开:
CDerived()
{
CBase * p = this;
p-> x = 456; //错误:无法访问受保护的成员


在此上下文中,编译器不知道''p'实际上是''CDerived'类型的
。因此,它假设你可能会玩兄弟姐妹的私人部分。我怀疑这个

在现实生活中是否会受到赞赏,因此在C ++中也不允许使用
。如果我没记错的话,其他

语言的作者对此有不同的态度...


BTW:void main()


根据C标准,上述声明是非法的:

''main()''必须返回''int''。

{
printf(" CBase =%d \ nCDerived =%d \ n",CBase(),CDerived());
}


将参数传递给

a变量参数列表时不会应用隐式转换。

我不明白为什么CDerived不能访问整数x ; CBase的
没有让CBase宣布CDerived为朋友。可以
有人向我解释为什么会这样?谢谢。




实际上,常见问题解答也回答了这个问题。

-

<的mailto:二*********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.contendix.com> - 软件开发&咨询


xxx写道:


我有点麻烦理解为什么衍生类不能通过以下代码访问基类的受保护成员:

#include< stdio.h>

类CBase
{
protected:
int x;
public:
CBase(){x = 123;}
public:
operator int(){return x ;}
};

课程CDerived:public CBase
{
公开:
CDerived()
{/> CBase * p = this;
p-> x = 456; //错误:无法访问受保护的成员


只需写:

x = 456;

}
};

void main()


main()必须返回int。

{
printf(" CBase =%) d\\\
CDerived =%d \ n",CBase(),CDerived());


你不能通过变量参数列表传递对象,而编译器

不知道它必须使用你的运算符int(),所以你在将对象提交给printf之前,必须将你的对象转换为int:


printf(" CBase =%d \ nCDerived =%d \ n" ;,static_cast< int>(CBase()),

static_cast< int>(CDerived()));


或者只是使用cout(#include < iostream>在顶部):


std :: cout<< " CBase的= QUOT; << CBase()<< " \\\
CDerived = QUOT; << CDerived()<< " \ n";


}




> #include< stdio.h>


我知道这是合法的,但不应该是#include< cstdio> ?


好​​奇。


干杯,

Matthias



I''m having a little trouble understanding why a derivative class cannot
access a protected member of the base class in the following code:
#include <stdio.h>

class CBase
{
protected:
int x;
public:
CBase () {x = 123;}
public:
operator int () {return x;}
};

class CDerived : public CBase
{
public:
CDerived ()
{
CBase* p = this;
p->x = 456; // error: cannot access protected member
}
};

void main ()
{
printf ("CBase=%d\nCDerived=%d\n", CBase (), CDerived ());
}
I don''t see why CDerived shouldn''t be able to access the integer "x" in
CBase without having CBase to declare CDerived as a friend. Can someone
explain to me why this is? Thank you.

解决方案

xxx wrote:

class CBase
{
protected:
int x;
};

class CDerived : public CBase
{
public:
CDerived ()
{
CBase* p = this;
p->x = 456; // error: cannot access protected member
In this context the compiler does not know that ''p'' is actually
of type ''CDerived''. Thus, it assumes that you are possibly
playing with the private parts of a sibling. I doubt that this
would be appreciated in real life and is consequently also not
allowed in C++. If I remember correctly, authors of other
languages have a different attitude towards this...

BTW: void main ()
The above declaration is illegal according to the C standard:
''main()'' has to return an ''int''.
{
printf ("CBase=%d\nCDerived=%d\n", CBase (), CDerived ());
}
Implicit conversions are not applied when passing arguments to
a variable parameter list.
I don''t see why CDerived shouldn''t be able to access the integer "x" in CBase without having CBase to declare CDerived as a friend. Can someone explain to me why this is? Thank you.



Actually, this question is also answered in the FAQ.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


xxx wrote:


I''m having a little trouble understanding why a derivative class cannot
access a protected member of the base class in the following code:
#include <stdio.h>

class CBase
{
protected:
int x;
public:
CBase () {x = 123;}
public:
operator int () {return x;}
};

class CDerived : public CBase
{
public:
CDerived ()
{
CBase* p = this;
p->x = 456; // error: cannot access protected member
Just write:
x = 456;
}
};

void main ()
main() must return int.
{
printf ("CBase=%d\nCDerived=%d\n", CBase (), CDerived ());
You cannot pass objects through variable argument list, and the compiler
doesn''t know that it has to use your operator int(), so you have to cast
your objects to int before giving them to printf:

printf ("CBase=%d\nCDerived=%d\n", static_cast<int>(CBase ()),
static_cast<int>(CDerived ()));

Or just use cout (#include <iostream> at the top):

std::cout << "CBase=" << CBase() << "\nCDerived=" << CDerived() << "\n";

}




> #include <stdio.h>

I know that''s legal, but shouldn''t this rather be #include <cstdio> ?

Just curious.

Cheers,
Matthias


这篇关于多态性和受保护的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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