问题标签 [dynamic-cast]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
7 回答
1175 浏览

c# - 我应该使用 dynamic_cast用于复制?

更新1:

更正了胡说八道的代码!感谢您的评论,我对第一个片段进行了哈希处理,哎呀。

更新 2:

还更新了问题标题,因为已指出答案没有必要使用 dynamic_cast。

我在这里想要实现的是使用强类型的深层副本;我希望能够将 Class2 复制到 Class2 的另一个实例;但是,我也想使用基础 Class1 中的 CopyTo 函数。这个想法来自我的 C# 经验,通常我只是将返回类型设为通用(参见 C# 片段)。

这就是我在 C# 中的方式:

现在,与 C# 片段相比,C++ 片段感觉很臭。是否可以在没有指针的情况下执行此操作,或者这种方式是最佳实践?

0 投票
4 回答
32742 浏览

c++ - 将 dynamic_cast 与引用和指针一起使用时的行为差异

我正在检查 dynamic_cast 的行为,发现当它失败时,只有当目标是引用类型时才会抛出 std::bad_cast 异常。如果目标是指针类型,则转换不会引发异常。这是我的示例代码:

输出是“Caught bad cast”和“NULL pointer”。代码使用 VS2008 编译。这是正确的行为吗?如果是,那么为什么会有差异?

0 投票
4 回答
6951 浏览

c++ - 用于遍历继承层次结构的静态转换与动态转换

我看到一本关于 C++ 的书提到,使用静态转换导航继承层次结构比使用动态转换更有效。

例子:

但是,动态转换和静态转换(如上面实现的)都需要启用 RTTI 才能使此类导航正常工作。只是动态转换需要类层次结构是多态的(即基类至少有一个虚函数)。
静态演员的这种效率提升从何而来?这本书确实提到动态转换是进行类型安全向下转换的首选方式。

0 投票
3 回答
742 浏览

c++ - Acceptable to use virtual inheritance to prevent accidentally creating a diamond?

This is a simplification of some real code, and a real mistake I made when I didn't realize someone else had already implemented Foo and derived from it.

EDIT: to save you from having to run this code...

  • If run as-is, it prints "fail" (undesireable, hard to debug).
  • If you delete the line marked "oops" it prints "Foo" (desired behavior).
  • If you leave the "oops" and make the two inheritances virtual, it won't compile (but at least you know what to fix).
  • If you delete the "oops" and make them virtual, it will compile and will print "Foo" (desired behavior).

With virtual inheritance, the outcomes are either good or compiler-error. Without virtual inheritance, the outcomes are either good or unexplained, hard-to-debug runtime failure.


When I implemented Bar, which basically duplicated what Foo was already doing, it caused the dynamic cast to fail, which meant bad things in the real code.

At first I was surprised there was no compiler error. Then I realized there was no virtual inheritance, which would have triggered the 'no unique final overrider' error in GCC. I purposefully chose not to use virtual inheritance since there aren't supposed to be any diamonds in this design.

But had I used virtual inheritance when deriving from Base, the code would have worked just as well (without my oops), and I would have been warned about the diamond at compile time vs. having to track down the bug at run time.

So the question is -- do you think it's acceptable to use virtual inheritance to prevent making a similar mistake in the future? There's no good technical reason (that I can see) for using virtual inheritance here, since there should never be a diamond in the design. It would only be there to enforce that design constraint.

0 投票
3 回答
1143 浏览

interface - COM 对象到 COM 接口的 dynamic_cast 不会增加引用计数,是吗?

如果我有一个 C++ 类 X,它实现了 COM 接口 IY 和 IZ,并且我有一个指向 X 类型对象的 IY 接口的指针 y,我这样做:

这不会影响对象的引用计数,是吗?我不必做一个 Release() 来解释它,对吧?

如果重要的话,我正在使用 ATL/COM。

我猜答案是“不,它不会增加引用计数,不,你不必 Release()”,但我想确定一下。

提前致谢。

0 投票
3 回答
1414 浏览

c++ - Is LLVM an exception to the rule for avoiding dynamic casts?

LLVM has it's own hand rolled alternative to RTTI that is a speed improvement over built-in RTTI and allows dynamic casting to classes with no vtable (dyn_cast). However, it can still be used in exactly the way that dynamic_cast<> is used though it does allow it to be used with more classes.

dyn_cast<> template documentation

LLVM is a reputable C++ project so this seems to fly in the face of the common saying that too many dynamic casts is a sign of bad design, also known as a code smell. Surely a better performing dynamic cast does nothing to improve its use in design than a standard dynamic_cast. So who is right here? Are there cases where large-scale use of dynamic casting is a good design choice in C++ code? Google turns up 690 occurrences of this kind of dynamic casting in the LLVM trunk source code.

Uses of dyn_cast<> in LLVM trunk

0 投票
3 回答
1643 浏览

function - 调用接受基类的函数时是否需要使用 dynamic_cast?

我有一些这样的课程:

目前我总是在调用这样的函数时尝试使用动态转换,(上面的#1)。
但是它确实使代码非常难看,所以我想知道它是否真的有必要。

您是否以这种方式使用dynamic_cast?如果有,主要原因是什么?

0 投票
1 回答
420 浏览

c++ - 这是对dynamic_cast的正确使用吗?

我有三个类:Generic、CFG 和 Evaluator。

这是通用的:

这是CFG:

并且 Evaluator 没有子类化。

我提供了一个名为 PluginLCD 的 DLL,它有一个名为 Connect 的方法:

以下是我通过 scons 编译 DLL 的方式:

现在,我的代码中有两种情况。一个是在类LCDControl,哪个子类CFG。另一种情况是上面 whereGeneric子类EvaluatorCFG. Evaluator 有一个名为 LoadPlugins 的方法,正如其名称所暗示的那样,this通过 method 传递给 DLL Connect。好吧,在第一种情况下Generic *, Connect 中的演员应该返回NULL。但是,在第二种情况下,据我所知,应该返回一个有效的指针。它似乎不会以这种方式发生。我错了吗?

0 投票
3 回答
35437 浏览

c++ - c++中的动态转换到底是什么

谁能告诉 C++ 中的动态转换到底是什么意思。我们究竟可以在哪里使用这种动态转换?这是在面试中问我的,我对这个问题一无所知:)。

0 投票
8 回答
1518 浏览

c++ - C++:将整数转换为指针的安全方法

我需要将包含地址的整数类型转换为实际的指针类型。我可以使用 reinterpret_cast 如下:

但是,这不会执行任何运行时检查以查看所讨论的地址是否实际上包含 MyClass 对象。我想知道首先转换为 void*(使用 reinterpret_cast)然后在结果上使用 dynamic_cast 是否有任何好处。像这样:

使用第二种方法有什么好处吗?