本文介绍了VC ++ 2005:私有虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得不到的东西:


我们说:


ref class Base {

virtual void SomeVirtualFunction()

{Console :: WriteLine(L" Base");}

public:

void SomeAccessibleFunction()

{SomeVirtualFunction();}

};


ref class派生:public Base {

virtual void SomeVirtualFunction()

{Console :: WriteLine(L" Derived");}

};


int main(array< System :: String ^> ^ args)

{

Base ^ handle = gcnew Derived();

handle- > SomeAccessibleFunction();


返回0;

}


我希望得到Derived ;结果吧?但是,我得到了

" Base"以及编译时的奇怪警告:


" .\PrivateVirtual.cpp(8):警告C4486: ''Base :: SomeVirtualFunction'':

a ref类或值类的私有虚方法应该标记

''密封''

.. \ _PrivateVirtual.cpp(16):警告C4486:

''派生:: SomeVirtualFunction'':参考的私有虚方法

类或值课程应标记为''密封''

"


如果我尝试明确覆盖:


。 .\PrivateVirtual.cpp(17):错误C3671:''派生:: SomeVirtualFunction''

:函数不会覆盖''Base :: SomeVirtualFunction''

..\PrivateVirtual.cpp(16):警告C4486:

''派生:: SomeVirtualFunction'':参考的私有虚方法

类或值类应该标记为''密封''


这里发生了什么?

Something I don''t get it:

Say we have:

ref class Base {
virtual void SomeVirtualFunction()
{Console::WriteLine(L"Base");}
public:
void SomeAccessibleFunction()
{SomeVirtualFunction();}
};

ref class Derived : public Base {
virtual void SomeVirtualFunction()
{Console::WriteLine(L"Derived");}
};

int main(array<System::String ^> ^args)
{
Base^ handle = gcnew Derived();
handle->SomeAccessibleFunction();

return 0;
}

I would expect to get "Derived" as the result, right? However, I get
"Base", and strange warnings during compilation:

".\PrivateVirtual.cpp(8) : warning C4486: ''Base::SomeVirtualFunction'' :
a private virtual method of a ref class or value class should be marked
''sealed''
..\PrivateVirtual.cpp(16) : warning C4486:
''Derived::SomeVirtualFunction'' : a private virtual method of a ref
class or value class should be marked ''sealed''
"

If I try explicit overriding:

..\PrivateVirtual.cpp(17) : error C3671: ''Derived::SomeVirtualFunction''
: function does not override ''Base::SomeVirtualFunction''
..\PrivateVirtual.cpp(16) : warning C4486:
''Derived::SomeVirtualFunction'' : a private virtual method of a ref
class or value class should be marked ''sealed''

What is going on here?

推荐答案




" private"虚函数没用!因为没有人可以覆盖它!


你需要声明你的虚拟功能至少为受保护!


-

问候

Jochen


关于Win32和.NET的博客





"私人"虚函数没用!因为没有人可以覆盖它!

你需要声明你的虚拟功能至少为受保护!



"private" virtual functions are useless! because nobody can overwrite
it!

You need to declare your virtual functions at least as "protected"!




但是请注意,这仅适用于托管代码。在真正的C ++中,它将会工作!
Bo Persson



But note that this is so for managed code only. In real C++ it would
have worked!
Bo Persson


这篇关于VC ++ 2005:私有虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:36