为什么我能够访问我的私人功能 [英] How come I am able to access my private function

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

问题描述

嗨所有,


我只是想看看程序的o / p。让我知道wt是

的原因。

类底座

{

public:

虚拟空虚f1()

{

cout<< Base :: f1()"<< endl;

}

};


class派生:公共基地

{

私人:

void f1()

{

cout<< Derived :: f1()"<< endl;

}

};


int main( )

{

Base * bp = new Derived;

bp-> f1();


返回0;

}

它输出派生的私有函数。

是什么原因,这是否就像打破OOPS一样。


-Nivvy

HI all,

I just wondered by seeing the o/p of the program. Let me know wt is the
reason.

class Base
{
public :
virtual void f1()
{
cout << "Base::f1() "<<endl;
}
};

class Derived : public Base
{
private:
void f1()
{
cout << "Derived::f1() "<<endl;
}
};

int main()
{
Base *bp = new Derived;
bp->f1();

return 0;
}
It ouputs the Derived private function.
What is the reason, whether this is like breaking OOPS.

-Nivvy

推荐答案

不,你没有打破OOPS。编译器的编写者是无能为力的
。在更改任何内容时,您应该会收到错误。

重新定义虚拟功能,包括其访问权限。


问候,

博士Z.

首席科学家
zo****@ZHMicro.com http://www.zhmicro.com
http://distributed-software.blogspot.com

No, you are not breaking OOPS. The writers of the compiler are
incompetent. You should get an error when changing anything in
redefining a virtual function, including its access.

Regards,
Dr. Z.
Chief Scientist
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com


多数关于伙伴的虚拟功能。您已将其声明为

virtual,并且编译器已为您的函数f1()分配了VPTR。现在

当你试图访问通过基类指针派生的类型的f1时你会因为VPTR指向派生的f1而在派生类中登陆。

要验证只是声明另一个私有函数在派生说f2和

尝试访问。它不会。

记住只有基类中的虚函数并且在派生的

中实现才能以这种方式访问​​。

Thats what virtual function is all about buddy. You have declared it as
virtual and compiler has assigned a VPTR for your function f1(). Now
when you try to access f1 of type derived via base class pointer you
will land up in derived class since VPTR points to derived f1.
To verify just declare another private function in derived say f2 and
try to access. It will not.
Remember only virtual function in base class and implemented in derived
are accesible in this manner.




" Nivvy" <已经******* @ gmail.com>在消息中写道

"Nivvy" <ve*******@gmail.com> wrote in message
HI all,

我只是想看看程序的o / p。让我知道wt是
的原因。
类基地
{
公开:
虚拟空虚f1()
{
cout<< Base :: f1()"<< endl;
}
};

类派生:公共基地
{
私人:
void f1()
{
cout<< " Derived :: f1()"<< endl;
}
};

int main()
{*基* bp = new Derived;
bp-> f1();
HI all,

I just wondered by seeing the o/p of the program. Let me know wt is the
reason.

class Base
{
public :
virtual void f1()
{
cout << "Base::f1() "<<endl;
}
};

class Derived : public Base
{
private:
void f1()
{
cout << "Derived::f1() "<<endl;
}
};

int main()
{
Base *bp = new Derived;
bp->f1();




bp的静态(编译时)类型是Base *。 f1在Base中公开定义,

所以它可以访问它。在运行时,bp的类型是Derived *,因此调用

派生类函数。访问说明符只在编译时才会显示




Sharad



The static (compile time) type of bp is Base*. f1 is defined public in Base,
so it can access it. At run time the type of bp is Derived* and hence the
derived class function is invoked. Access specifiers come into picture only
at compile time.

Sharad


这篇关于为什么我能够访问我的私人功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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