覆盖虚拟功能澄清...... [英] Overriding Virtual Function clarification...
问题描述
&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培; &&&&&&&&&&&&&&
#include< iostream>
使用命名空间std;
class A
{
int i;
virtual void f (){cout<<< endl<<" i ="<< i; }
public:
A(int j){i = j; }
};
B级:公共A
{
int j;
virtual void g(){cout<< endl<<" j ="<< j; }
public:
virtual void f(){cout<<"我在B :: f()" ;; }
B(int k):A(k + 10){j = k; }
};
int main()
{
A * a_base;
B * b_derived;
a_base = new B(1);
a_base-> f();
}
&&&&&&&&&&&&&&&&& amp ;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培; &&&&&&&&&&&&&&
在上面的代码中,A中的虚函数声明导致了一个
编译器错误,因为它是私有的。这是否意味着虚拟
函数覆盖也要求基础delaration也是公开的。
请解释一下......因为我之前没有尝试过这个。 />
-praveen。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
#include<iostream>
using namespace std;
class A
{
int i;
virtual void f(){ cout<<endl<<"i= "<<i; }
public:
A(int j) { i = j; }
};
class B : public A
{
int j;
virtual void g() { cout<<endl<<"j= "<<j; }
public:
virtual void f(){ cout<<"I''m in B::f()"; }
B(int k):A(k+10){ j = k; }
};
int main()
{
A* a_base;
B* b_derived;
a_base = new B(1);
a_base->f();
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
Hi,
In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn''t tried this earlier.
-praveen.
推荐答案
>
在在上面的代码中,A中的虚函数声明导致了一个
编译器错误,因为它是私有的。这是否意味着虚拟
功能覆盖也要求基础认证也是公开的。
请解释一下......因为我之前没有尝试过。
-praveen 。
In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn''t tried this earlier.
-praveen.
私有基类成员函数无法访问派生的
类,请尝试使它们受到保护。
Tobias
-
重要提示:此电子邮件和附件的内容是保密的
并且可能需要合法特权和/或受版权保护。
禁止将其任何部分复制或传播给他人,并且可能是非法的。
Private base class member functions are not accessable to derived
classes, try to make them protected instead.
Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Tobias Blomkvist写道:
Tobias Blomkvist wrote:
在上面的代码中,虚函数声明在A因私有而导致编译错误。这是否意味着虚拟
功能覆盖也要求基础认证也是公开的。
请解释一下......因为我之前没有尝试过。
-praveen 。
私有基类成员函数不能访问派生的类,试着让它们受到保护。
托比亚斯
In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn''t tried this earlier.
-praveen.
Private base class member functions are not accessable to derived
classes, try to make them protected instead.
Tobias
谢谢......好吧,我在发布之前试过了,并且
成功了。但我对背后的原因感兴趣...
-praveen -
重要提示:此电子邮件和附件的内容是保密的
并且可能受到法律特权和/或受版权保护。
禁止将其任何部分复制或传播给他人,并且可能是非法的。
Hi,
Thanks..Thats fine, I tried that before posting here and was
successful. but I am interested in reason behind...
-praveen --
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Achintya在新闻中写道:1122128095.067742.271170
@ g43g2000cwa.googlegroups.com in comp.lang.c ++:
Achintya wrote in news:1122128095.067742.271170
@g43g2000cwa.googlegroups.com in comp.lang.c++:
int main( )
{A / a_base;
B * b_derived;
a_base = new B(1);
a_base-> f();
&&&&&&&&&&&&&&&&&&&&& amp ;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培;&安培; &&&&&&&&&&&&&&
在在上面的代码中,A中的虚函数声明导致了一个
编译器错误,因为它是私有的。这是否意味着虚拟
功能覆盖也要求基础delaration也是公开的。
请解释......因为我之前没有尝试过。
int main()
{
A* a_base;
B* b_derived;
a_base = new B(1);
a_base->f();
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
Hi,
In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn''t tried this earlier.
不,当你从main()调用A :: f()时需要public,即需要公共访问A :: f()的
上下文。 br />
当你调用A :: f()时编译器不在乎
是否a :: f()是虚拟的,或者如果它被覆盖了,只有
检查A :: f()的可访问性。
事实上,在这种情况下a_base实际指向一个B
一个可访问的f()doesen'不重要。类型和访问检查是由编译器在编译时完成的,而不是在运行时,并且在
一般情况下,编译器不知道动态类型(B - 以上)
一个指针(A * a_base - 上面)也指向。
HTH。
Rob。
-
http:/ /www.victim-prime.dsl.pipex.com/
这篇关于覆盖虚拟功能澄清......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!