What are some 'good use' examples of dynamic casting?

We often hear/read that one should avoid dynamic casting. I was wondering what would be 'good use' examples of it, according to you? Edit: Yes, I'm aware of that other thread: it is indeed when reading one of the first answers there that I asked my question! This recent thread gives an example of where it comes in handy. There is a base Shape class and classes Circle and Recta

什么是动态铸造的一些“很好用”的例子?

我们经常听到/读到应该避免动态投射。 根据你的看法,我想知道什么是“好用”的例子? 编辑: 是的,我知道另一个线程:当我读到我问的第一个答案时,确实是这样。 最近的这个线索给出了一个它派生出来的例子。 有一个基本的Shape类和从它派生的类Circle和Rectangle。 在测试平等时,很显然Circle不能等于Rectangle,试图比较它们将是一场灾难。 在遍历指向Shapes的指针集合时,dynamic_cast会执行双重任务,告诉您这些

Why does c++ forbid implicit conversion of void*?

This question already has an answer here: Do I cast the result of malloc? 27 answers In C++, unlike in C, you have to cast the result of malloc. Your code can be tweaked into working order with a simple cast. int *a = (int *)malloc(sizeof(int)); A great article about this mandatory cast and the reasons behind it can be found here. An additional link for reference can be found here. Ed

为什么c ++禁止void *的隐式转换?

这个问题在这里已经有了答案: 我输入malloc的结果吗? 27个答案 在C ++中,与C不同,您必须投射malloc的结果。 您的代码可以通过简单的演员调整为工作顺序。 int *a = (int *)malloc(sizeof(int)); 关于这个强制演员的一篇很好的文章及其背后的原因可以在这里找到。 可以在这里找到一个供参考的附加链接。 编辑:正如评论中所建议的, malloc()的使用不应该是司空见惯的。 最接近的选择是使用new分配。 int *a = n

Typecasting malloc C++

This question already has an answer here: Do I cast the result of malloc? 27 answers when and why is typecasting a call to malloc neccessary in C++? Always when not assigning to a void * , since void * doesn't convert implicitly to other pointer types, the way it does in C. But the true answer is you shouldn't ever use malloc in C++ in the first place. I am not suggesting you sho

类型malloc C ++

这个问题在这里已经有了答案: 我输入malloc的结果吗? 27个答案 何时以及为什么要在C ++中调用malloc需求? 总是在不分配给void * ,因为void *不会将其隐式转换为其他指针类型,而是它在C中的方式。但真正的答案是, 您不应该首先在C ++中使用malloc 。 我不是建议你应该使用new而不是malloc 。 现代C ++代码应该谨慎使用new代码,或者尽可能避免使用new代码。 你应该隐藏所有使用new或使用非原始类型(如Xeo提到的

Does constexpr in pointers make a difference

What is the difference between constexpr int *np = nullptr and int const *np = nullptr ? np is a constant pointer to an int that is null, in both cases. Is there any specific use of constexpr in context of pointers. If you try to do anything with the pointer and use the result in a constant expression, then the pointer must be marked as constexpr. The simple example is pointer arithmetic, o

指针中的constexpr是否有所作为?

constexpr int *np = nullptr和int const *np = nullptr之间有什么区别? 在这两种情况下, np都是一个指向int的常量指针。 在指针的上下文中是否有特定的constexpr用法。 如果您试图对指针进行任何操作并在常量表达式中使用结果,则指针必须标记为constexpr。 简单的例子是指针算术或指针取消引用: static constexpr int arr[] = {1,2,3,4,5,6}; constexpr const int *first = arr; constexpr const int *second = first

c++

I got a comment to my answer on this thread: Malloc inside a function call appears to be getting freed on return? In short I had code like this: int * somefunc (void) { int * temp = (int*) malloc (sizeof (int)); temp[0] = 0; return temp; } I got this comment: Can I just say, please don't cast the return value of malloc? It is not required and can hide errors. I agree that the

C ++

我对这个主题的回答有了一个评论: 函数调用中的malloc似乎在返回时被释放? 总之,我有这样的代码: int * somefunc (void) { int * temp = (int*) malloc (sizeof (int)); temp[0] = 0; return temp; } 我得到了这个评论: 我可以说,请不要施加malloc的返回值? 这不是必需的,可以隐藏错误。 我同意C中不需要强制转换。它在C ++中是强制性的,所以我通常添加它们以防万一我必须在C ++中移植代码。 但是,

Heap array allocation instead of on stack

I'm implementing the sieve of eratosthenes algorithm in C++, and I've run into a problem. When I initialize my array to a very large value, like 1 million, it breaks because I'm allocating too large of an array to the stack. The answer in C would be to use malloc like this Sieve of Eratosthenes , but this solution does not work in C++ (to the best of my knowledge). Any ideas on how

堆数组分配而不是堆栈

我在C ++中实现了eratosthenes算法的筛选,并且遇到了一个问题。 当我将我的数组初始化为一个非常大的值(例如100万)时,它会因为我将大数组分配给堆栈而中断。 在C中的答案是使用像这个Eratosthenes Siebel一样的malloc,但是这个解决方案在C ++中并不工作(据我所知)。 关于如何通过在堆而不是堆栈中分配数组来获得此程序的大量数据的任何想法? 谢谢。 要查看我遇到的问题,请更改下面的代码int integerList [1000],

When should you use a class vs a struct in C++?

在哪种情况下最好在C ++中使用struct与class ? Differences between a class and a struct in C++ are that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance and can have member functions. I would recommend using structs as plain-old-data structures without a

什么时候应该在C ++中使用类和结构?

在哪种情况下最好在C ++中使用struct与class ? C ++中class和struct之间的区别在于,结构具有默认的public成员,并且基类和类具有默认的private成员和基础。 类和结构都可以有public成员和private成员的混合,可以使用继承并可以具有成员函数。 我会推荐使用结构作为普通的旧数据结构,没有任何类特征,并将类用作具有private数据和成员函数的聚合数据结构。 正如其他人所指出的,实际上只有两种语言差异: struct默认为

Compiler warnings with MySQL++ with release configuration

For some reason I get some warnings about "non dll-interface class" when building with a release configuration, but not debug. I've compared the release and debug configurations, and my ones with the MySQL++ example ones, however I cant see which setting is causing these warnings. 1>c:sqlmysql 5.0mysql++-3.0.9libqparms.h(49) : warning C4275: non dll-interface class 'std::_Cont

带有发布配置的MySQL ++编译器警告

出于某种原因,当使用发布配置构建时,我收到了一些有关“非dll-interface类”的警告,但未进行调试。 我比较了发布和调试配置,以及我的例子与MySQL ++的例子,但是我不知道哪些设置导致了这些警告。 1>c:sqlmysql 5.0mysql++-3.0.9libqparms.h(49) : warning C4275: non dll-interface class 'std::_Container_base_aux' used as base for dll-interface class 'std::_Container_base_aux_alloc_real<_Alloc>' 1>

How to deal with noexcept in Visual Studio

I'm trying to create a custom exception that derives from std::exception and overrides what() . At first, I wrote it like this: class UserException : public std::exception { private: const std::string message; public: UserException(const std::string &message) : message(message) {} virtual const char* what() const override { return message.c_str();

如何在Visual Studio中处理noexcept

我试图创建一个派生自std::exception的自定义std::exception并重写what() 。 起初,我是这样写的: class UserException : public std::exception { private: const std::string message; public: UserException(const std::string &message) : message(message) {} virtual const char* what() const override { return message.c_str(); } }; 这在VS2012中工作正常,但它不能

Using wxString with Google Mock

Has anyone out there had any luck using Google Mock in conjunction with wxWidgets? I have a class Foo with setters that take a const reference to wxString in the signature like so: class Foo { public: Foo(); virtual ~Foo(); void setName(const wxString& name); }; I then proceed to mock Foo like this: class MockFoo : public Foo { MOCK_METHOD1(setName, void(const wxString&

在Google Mock中使用wxString

使用Google Mock与wxWidgets一起使用有没有人有过运气? 我有一个带有setter的Foo类,在签名中采用对wxString的const引用,如下所示: class Foo { public: Foo(); virtual ~Foo(); void setName(const wxString& name); }; 然后我继续像这样模拟Foo: class MockFoo : public Foo { MOCK_METHOD1(setName, void(const wxString& name)); }; 我的其他模拟工作正常,但有一些关于它不喜欢的wxString