关于dynamic_cast<>() [英] about dynamic_cast<>()

查看:50
本文介绍了关于dynamic_cast<>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里!


当我将dynamic_cast用于单个对象时,我会做类似的事情:


void foo(Base * base)

{

if(dynamic_cast< Derived *>(base)!= NULL){

((Derived *)base) - > do_something()

}

}


我知道最好避免滥用dynamic_cast,用户虚拟

功能,等等。


但是,我认为能够写出更实用的东西是
喜欢:

if(istypeof< Derived>(base)){

}


我找到了dynamic_cast风格(实际上,所有''* _cast<>"),要花很多的时间来写一些字符,例如,这在

格式中特别烦人。 />

在新的

标准的提案中是否有类似的内容?

(或者可能已经有类似的内容了当前

标准)

Baltasar

Hi, there !

When I use dynamic_cast for a single object, I do something like:

void foo(Base *base)
{
if ( dynamic_cast<Derived *>( base ) != NULL ) {
((Derived *) base)->do_something()
}
}

I know it''s better to avoid abuse of dynamic_cast, user virtual
functions, and so on.

However, I think it would be more practical to be able to write
something like:
if ( istypeof<Derived>( base ) ) {
}

I find the dynamic_cast style (actually, all ''*_cast<>"), to take too
much characters to be written, and this is specially annoying in
formatting, for example.

Is there somehting like this within the proposals for the new
standard ?
(or maybe there is already something like this in the current
standard)

Regards,

Baltasar

推荐答案

ba ******* @ gmail.com 写道:

当我将dynamic_cast用于单个对象时,我会执行以下操作:


void foo(Base * base)

{

if(dynamic_cast< Derived *>(base)!= NULL){

((Derived *)base) - > do_something()

}
When I use dynamic_cast for a single object, I do something like:

void foo(Base *base)
{
if ( dynamic_cast<Derived *>( base ) != NULL ) {
((Derived *) base)->do_something()
}



更好(惯用)的方式是


if(Derived * derived = dynamic_cast< Derived *>(base)){

派生 - > do_something();

}


当然最好的办法是''do_something' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''你只需要写一下


base-> do_something();

A better (idiomatic) way would be

if (Derived *derived = dynamic_cast<Derived*>(base)) {
derived->do_something();
}

Of course the best way would be to have ''do_something'' declared
''virtual'' and overridden in ''Derived'', so that you don''t need to
find out what real type the object was. You''d just write

base->do_something();


}

我知道最好避免滥用dynamic_cast,用户虚拟

函数等等。
}

I know it''s better to avoid abuse of dynamic_cast, user virtual
functions, and so on.



哦......很好。所以你可能也知道

中提到的通常笑话这样的情况:


病人:医生,如果我做*这个*,就会疼。

医生:不要这样做。

Oh... Good. So you probably also know the usual joke mentioned in
such a situation:

Patient: "Doctor, if I do *this*, it hurts."
Doctor: "Don''t do that."


但是,我觉得做起来更实际能写如下:

if(istypeof< Derived>(base)){

}
However, I think it would be more practical to be able to write
something like:
if ( istypeof<Derived>( base ) ) {
}



....然后是什么?它是如何更实用的?

....and then what? And how is it more practical?


我发现dynamic_cast风格(实际上,所有''* _cast<>"'),也是如此。

要写的多个字符,例如,这在

格式中特别烦人。
I find the dynamic_cast style (actually, all ''*_cast<>"), to take too
much characters to be written, and this is specially annoying in
formatting, for example.



所以,你试图避免腕管综合征,是吗?


如你所说,如果你滥用''dynamic_cast''或者出于某种原因设计

你的班级错了,你会被更多的打字困住。只是

想象''do_something''是虚拟的。很高兴

简单地省略所有'if'和''dynamic_cast''等......

So, you''re trying to avoid carpal tunnel syndrome, is that it?

As you say, if you abuse ''dynamic_cast'' or for some reason design
your classes wrong, you will be stuck with much more typing. Just
imagine that ''do_something'' is virtual. What a pleasure it is to
simply omit all the ''if'' and ''dynamic_cast'' and such...


在新的

标准的提案中是否有类似的内容?
Is there somehting like this within the proposals for the new
standard ?



Nope。

Nope.


(或许现在已经有类似的东西了

标准)
(or maybe there is already something like this in the current
standard)



Nope。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Nope.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


ba******* @ gmail.com 写道:
ba*******@gmail.com wrote:

那里!


当我将dynamic_cast用于单个对象时,我会做类似的事情:


void foo(Base * base)

{

if(dynamic_cast< Derived *>(base)!= NULL){

((派生*)基数) - > do_something()

}

}


我知道最好避免滥用dynamic_cast,用户虚拟

函数等等。


然而,我认为这将更加实际cal能够写出

类似于:

if(istypeof< Derived>(base)){

}


我发现dynamic_cast风格(实际上,所有''* _cast<>""),要花太多

要编写多个字符,这特别令人讨厌

格式化,例如。
Hi, there !

When I use dynamic_cast for a single object, I do something like:

void foo(Base *base)
{
if ( dynamic_cast<Derived *>( base ) != NULL ) {
((Derived *) base)->do_something()
}
}

I know it''s better to avoid abuse of dynamic_cast, user virtual
functions, and so on.

However, I think it would be more practical to be able to write
something like:
if ( istypeof<Derived>( base ) ) {
}

I find the dynamic_cast style (actually, all ''*_cast<>"), to take too
much characters to be written, and this is specially annoying in
formatting, for example.



当您键入if

(istypeof< Derived>(base))时,它会产生多少不同?而不是if(dynamic_cast< Derived *>(base))?你

只保存了5个字符!我认为软件行业可能发生的最好的事情之一是现代编译器的自动兼容功能

(它使代码更具可读性)。你为什么不使用这个功能?


顺便说一句,在你的例子中,你必须将指针抛出两次:一次检查

如果 - 条件,一次用于实际使用铸造指针。这不是
的目的(一个正确的代码检查工具会抱怨C风格

无论如何)。您的代码应如下所示:


void foo(Base * base)

{

派生* derived = dynamic_cast<派生*>(基础);

if(派生){

衍生 - > do_something();

}

}


或者如果你是一个不想输入太多的人:


void foo(Base * base )

{

if(Derived * derived = dynamic_cast< Derived *>(base)){

derived-> do_something() ;

}

}


问候,

Stuart

How much of a different does it make when you type "if
(istypeof<Derived>(base))" instead of "if (dynamic_cast<Derived*>(base))"? You
only saved 5 characters! I think that one of the best things that could happen
to the software industry is the automatic complition feature of modern compilers
(it made code much more readable). Why don''t you use this feature?

BTW, in your examples, you have to cast the pointer twice: once for checking the
if-condition, and once for actually using the casted pointer. That is not how it
is intended to be (a proper code checking tool would complain about the C-style
cast, anyway). Your code should look like this:

void foo(Base *base)
{
Derived* derived = dynamic_cast<Derived *>( base );
if ( derived ) {
derived->do_something();
}
}

or if you are someone who doesn''t want to type too much:

void foo(Base *base)
{
if ( Derived* derived = dynamic_cast<Derived *>( base )) {
derived->do_something();
}
}

Regards,
Stuart


ba******* @ gmail.com 写道:
ba*******@gmail.com wrote:

那里!


当我将dynamic_cast用于单个对象时,我会做类似的事情:


void foo(Base * base)

{

if(dynamic_cast< Derived *>(base)!= NULL){

((派生*)基数) - > do_something()

}

}
Hi, there !

When I use dynamic_cast for a single object, I do something like:

void foo(Base *base)
{
if ( dynamic_cast<Derived *>( base ) != NULL ) {
((Derived *) base)->do_something()
}
}



为什么选择C风格演员?我更喜欢


void foo(Base * base){

Derived * ptr = dynamic_cast<派生*>(基础);

if(ptr!= 0){

ptr-> do_something();

} < br $>
}


想想,你甚至可以这样做:


void foo(Base * base){

if(Derived * ptr = dynamic_cast< Derived *>(base)){

ptr-> do_something();

}

}

Why the C-style cast? I would prefer

void foo ( Base* base ) {
Derived* ptr = dynamic_cast< Derived* >( base );
if ( ptr != 0 ) {
ptr->do_something();
}
}

In think, you could even do this:

void foo ( Base* base ) {
if ( Derived* ptr = dynamic_cast< Derived* >( base ) ) {
ptr->do_something();
}
}


我知道最好避免滥用dynamic_cast,用户虚拟

功能,等等。


但是,我认为能够写出像

这样的东西会更实际:

if(istypeof< Derived>(base)){

}


我找到了dynamic_cast风格(实际上,所有''* _cast<> ),要花很多的东西来写一些字符,例如,这在

格式中特别烦人。
I know it''s better to avoid abuse of dynamic_cast, user virtual
functions, and so on.

However, I think it would be more practical to be able to write
something like:
if ( istypeof<Derived>( base ) ) {
}

I find the dynamic_cast style (actually, all ''*_cast<>"), to take too
much characters to be written, and this is specially annoying in
formatting, for example.



我认为这是故意的:演员们坚持并且看起来很难看,因此他们不会掉以轻心。

不会掉以轻心。


此外,您可以自己轻松实现istypeof()函数。所以

什么在阻止你?

That, I think, is intentional: the casts stick and look ugly so that they
are not taken lightly.

Moreover, you can easily implement that istypeof() function yourself. So
what''s stoping you?


在新的提案中是否有类似这样的内容? >
标准?
Is there somehting like this within the proposals for the new
standard ?



我当然不希望如此,我也不知道。

I sure don''t hope so, and I am not aware of anything.


(或者在目前

标准中已经存在这样的事情)
(or maybe there is already something like this in the current
standard)



我没有注意到什么。

Best


Kai-Uwe Bux

Nothing that I was aware of.
Best

Kai-Uwe Bux


这篇关于关于dynamic_cast&lt;&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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