这是有效的C ++吗? [英] Is this valid C++?

查看:57
本文介绍了这是有效的C ++吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码编译最新的VC ++,而不是EDG或MINGW。

The following code compiles on the latest VC++, not EDG or MINGW.

展开 | 选择 | Wrap | 行号

推荐答案

你想做什么?


在Derived中有一种方法:


void doSomething(int i) ;


隐藏Base :: doSomething()。


如果你使用多态,这是一个错误,因为Derived :: doSomething( )永远不会使用Base指针或引用来调用。


另一方面,如果你正在进行基于对象的编程ramming,那么只要Derived :: doSomething()不必在内部调用Base :: doSomething()来恢复被Derived隐藏的Base中的代码,那就没关系。
What are you trying to do???

In Derived there is a method:

void doSomething(int i);

which hides Base::doSomething().

If you are using polymorphism, this is an error since Derived::doSomething() will never be called using a Base pointer or reference.

On the other hand, if you are doing object-based programming, then this would be OK so long as Derived::doSomething() did not have to internally call Base::doSomething() to recover the the code in Base that is being hidden by Derived.



你想做什么?


在Derived中有一种方法:

void doSomething(int i);


隐藏Base :: doSomething()。


如果使用多态,这是一个错误,因为Derived :: doSomething()永远不会使用Base指针或引用调用。
What are you trying to do???

In Derived there is a method:

void doSomething(int i);

which hides Base::doSomething().

If you are using polymorphism, this is an error since Derived::doSomething() will never be called using a Base pointer or reference.



实际上代码实际上不应该做任何事情,我只是尽量使我的代码示例尽可能简洁。目的是授予对doSomething()方法的访问权限,否则该方法将从Derived对象中隐藏,如下所示:

Actually the code is not really supposed to do anything, I just tried to make my code example as concise as possible. The intent is to grant access to the doSomething() method that would otherwise be hidden from a Derived object, like so:

展开 | 选择 | 换行 | 行号



" using Base :: doSomething()"没有提供模板参数或者您是否必须编写使用Base< T> :: doSomething()?
"using Base::doSomething()" without supplying a template parameter or do you have to write "using Base<T>::doSomething()"?



你使用编译的那个。

You use the one that compiles.


只是试图让我的代码示例尽可能简洁。目的是授予对doSomething()方法的访问权限,否则该方法将从Derived对象中隐藏,
just tried to make my code example as concise as possible. The intent is to grant access to the doSomething() method that would otherwise be hidden from a Derived object,



您可以这样编码。这不是推荐的,可以让你设置一个模糊的电话。但我已经看到了这个:

You can code this way. It''s not recommended and can set you up for an ambiguous call. But I have seen this done:

展开 | 选择 | Wrap | 行号


这篇关于这是有效的C ++吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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