本文介绍了与“虚拟基类”混淆概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我收到错误警告:b :: a_method隐藏虚拟功能

a :: a_method()。 br />

编译以下代码..


#include< iostream.h>

class a

{

public:

virtual int a_method()

{

cout< < "一个:: a_method" << endl;

返回0;

}

};


class b:public a

{

public:

int a_method(int x)

{

cout << " B :: a_method" << endl;

返回x;

}

};


class c:public a

{

public:

int a_method()

{

cout< < "Ç:: a_method" << endl;

返回100;

}

};


int main()

{

b b_var;

b_var.a_method(10);

返回0;

}


如果完成以下操作,警告就会消失:

更改课程b:公开a

to

class b:public virtual a


没有理解警告被删除的原因

" b :: a_method still隐藏a :: a_method()"


如果我能得到一些意见,会有所帮助。


谢谢和问候,

M Shetty


[见了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hi,

I get an error "Warning: b::a_method hides the virtual function
a::a_method()."

on compiling the following code..

#include <iostream.h>
class a
{
public:
virtual int a_method ()
{
cout << "a::a_method" << endl;
return 0;
}
};

class b : public a
{
public:
int a_method (int x)
{
cout << "b::a_method" << endl;
return x;
}
};

class c : public a
{
public:
int a_method ()
{
cout << "c::a_method" << endl;
return 100;
}
};

int main()
{
b b_var;
b_var.a_method(10);
return 0;
}

The warning goes off if done the following:
Change class b : public a
to
class b : public virtual a

Have not been to understand the reason why the warning is removed as
"b::a_method still hides a::a_method()"

Would help if I could get some input.

Thanks and Regards,
M Shetty

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案




Marshall Cline的C ++中涵盖了这个问题常问问题。请参阅主题

" [23.6]这是什么意思,警告:派生:: f(浮动)隐藏

Base :: f(int)? "在发布之前查看常见问题解答总是好的。

您可以在以下网址获取常见问题解答:


[23.6]含义是什么,警告:Derived :: f(float)隐藏
Base :: f(int)?在发布之前查看常见问题解答总是好的。
您可以在以下网址获取常见问题解答:




可能你错过了他的题。他确实知道这个函数是隐藏的b $ b,他的问题是关于警告在制作后被删除

A类虚拟基础。

To OP - 哪个编译器?即使在将它变成一个

虚拟基础之后,我也会收到一个警告 - 警告:function:a :: a_method()"被隐藏的

" b :: a_method" - 虚拟功能覆盖意图? int a_method(int x)"


-Sharad



Probably you are missing his question. He does know that the function is
being hidden, his question is about warnings getting removed after making
class A a virtual base.
To OP - Which compiler ? I get a warning with Comeau even after making it a
virtual base - "warning: function "a::a_method()" is hidden by
"b::a_method" -- virtual function override intended? int a_method (int x)"

-Sharad





我不知道为什么虚拟继承会删除它,但是当你在派生类(b :: a_method(int))中声明了一个

重载函数,它隐藏了所有与基类相同名称的
函数(a :: a_method( ))。例如,

你不能称之为b.a_method();如果你包括使用:: a_method;在b

定义中,a :: a_method不会被隐藏。顺便说一下,你可能想要把b
声称为a_method(int)也是虚拟的,不是吗?


-

Ivan


给我发电子邮件:korotkov2 at ztel dot ru

[见有关的信息]

[ comp.lang.c ++。主持。第一次海报:做到这一点! ]



I don''t know why virtual inheritance removes this, but when you declare an
overloaded function in derived class (b::a_method(int)) it hides all
functions with the same name from base class (a::a_method()). For example,
you can''t call b.a_method(); If you include using a::a_method; in b
definition, a::a_method won''t become hidden. Btw, you probably want to
declare a_method(int) as virtual, too, don''t you?

--
Ivan

e-mail me at: korotkov2 at ztel dot ru
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于与“虚拟基类”混淆概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:39