How to implement the factory method pattern in C++ correctly

There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple: How do I implement Factory Method in C++ correctly? Goal: to make it possible to allow the client to instantiate some object using factory methods instead of the object's constructors, without unacceptable conseq

如何正确实现C ++中的工厂方法模式

C ++中有这样一件事,使我长时间感到不舒服,因为我真的不知道该怎么做,尽管听起来很简单: 如何正确实现C ++中的Factory Method? 目标:使客户可以使用工厂方法而不是对象的构造函数实例化一些对象,而不会造成不可接受的后果和性能问题。 “工厂方法模式”是指对象内的静态工厂方法或其他类中定义的方法或全局函数。 一般来说,“将类X的实例化的正常方式重定向到构造函数以外的任何地方”的概念。 让我浏览一下我想到

How does this extra thread make it faster than unthreaded?

I just had a (to me) very odd observation and want to know how this can be. I tested the following two versions of code: chrono::steady_clock::time_point t1 = chrono::steady_clock::now(); process_data(l, 8); chrono::steady_clock::time_point t2 = chrono::steady_clock::now(); chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1); cout <

这个额外的线程如何使它比无螺纹更快?

我刚刚(对我)有一个非常奇怪的观察,并想知道这是怎么回事。 我测试了以下两个版本的代码: chrono::steady_clock::time_point t1 = chrono::steady_clock::now(); process_data(l, 8); chrono::steady_clock::time_point t2 = chrono::steady_clock::now(); chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1); cout << "time used: " << time_

What is the fastest way to create the union of many boost::polygons?

I have to union many boost::polgons, but my approach does not seem very performant (>15 min), especially with larger numbers of polygons (>2000). I push all the polygons i want to union into a multipolygon and then join the multipolygon, see my code: BOOST_FOREACH(polygon, multipolygon) { boost::geometry::clear(tmp_union); //tmp_union is a multipolygon boost::geometry::union_(resu

创建多个boost :: polygons的最快方法是什么?

我必须联合许多boost :: polgons,但是我的方法看起来并不是很高效(> 15分钟),尤其是对于更多数量的多边形(> 2000)。 我将所有想要合并的多边形推入多面体,然后加入多面体, 看我的代码: BOOST_FOREACH(polygon, multipolygon) { boost::geometry::clear(tmp_union); //tmp_union is a multipolygon boost::geometry::union_(result, poly, tmp_union); result = tmp_union; } 结果可能不包含非常多的

Memory Leaks not detected by CRT Memory Leak Detection

I got the Problem, that my application has an infinite growing Memory Leak, which is not detected. What I do very simplified, is to create an object, run a method on it and then delete the object. Each time I do this, the memory usage in the TaskManager grows by around 50-100MB. This exhausts my whole memory after some runs. I do this by multithreading, but there are no static variables, so t

内存泄漏未被CRT内存泄漏检测检测到

我得到了这个问题,我的应用程序有一个无限的内存泄漏,这是不被发现的。 我做的很简单,就是创建一个对象,在其上运行一个方法,然后删除该对象。 每次我这样做时,TaskManager中的内存使用量都会增长大约50-100MB。 这在一些运行后耗尽了我的整个记忆。 我通过多线程来做到这一点,但是没有静态变量,所以我的线程中的不同对象之间没有冲突。 它们只使用其他对象的静态方法,不会修改任何其他内存,而不会修改参数中的内

GCC memory leak detection equivalent to Microsoft crtdbg.h?

After many years of working on a general-purpose C++ library using the Microsoft MSVC compiler in Visual Studio, we are now porting it to Linux/Mac OS X (pray for us). I have become accustomed and quite fond of the simple memory leak detection mechanism in MSVC: #ifdef DEBUG #define _CRTDBG_MAP_ALLOC #define NEW new( _NORMAL_BLOCK, __FILE__, __LINE__) #include <stdlib.h>

GCC内存泄漏检测相当于微软的crtdbg.h?

经过多年在Visual Studio中使用Microsoft MSVC编译器开发通用C ++库后,我们现在将其移植到Linux / Mac OS X(为我们祈祷)。 我已经习惯于并且非常喜欢MSVC中简单的内存泄漏检测机制: #ifdef DEBUG #define _CRTDBG_MAP_ALLOC #define NEW new( _NORMAL_BLOCK, __FILE__, __LINE__) #include <stdlib.h> #include <crtdbg.h> #else #define NEW new #endif 每个内存分配都使用这个NEW

Memory leak (sort of) with a static std::vector

I have a static std::vector in a class. When I use Microsoft's memory leak detection tools: _CrtMemState state; _CrtMemCheckpoint( & state); _CrtMemDumpAllObjectsSince( & state ); it reports a leak after I insert stuff into the vector. This makes sense to me because new space is allocated when something is inserted into the vector. This space isn't deallocated until the progr

内存泄漏(有点)与静态std :: vector

我在一个类中有一个静态的std::vector 。 当我使用微软的内存泄漏检测工具时: _CrtMemState state; _CrtMemCheckpoint( & state); _CrtMemDumpAllObjectsSince( & state ); 它向插件插入东西后会报告泄漏。 这对我来说很有意义,因为在向量中插入某些内容时会分配新空间。 这个空间不会被释放,直到程序终止(因为矢量是静态的)。 这是正确的吗? 在包含向量的类的析构函数中,我将删除放入向量中的对象。 但

Is there an acceptable limit for memory leaks?

I've just started experimenting with SDL in C++, and I thought checking for memory leaks regularly may be a good habit to form early on. With this in mind, I've been running my 'Hello world' programs through Valgrind to catch any leaks, and although I've removed everything except the most basic SDL_Init() and SDL_Quit() statements, Valgrind still reports 120 bytes lost and 7

内存泄漏是否有可接受的限制?

我刚刚开始在C ++中尝试SDL,并且我认为定期检查内存泄漏可能是早期形成的良好习惯。 考虑到这一点,我一直在通过Valgrind运行我的'Hello world'程序来捕获任何泄漏,虽然除了最基本的SDL_Init()和SDL_Quit()语句之外,我已经删除了所有内容,但Valgrind仍然报告丢失了120个字节, 77k仍可到达。 我的问题是:是否有可接受的内存泄漏限制,还是应该努力使我的所有代码完全无泄漏? 小心Valgrind在测量中不会出现误

Difference in stringstream behavior for void* type using libc++ and libstdc++

The following test program returns different results depending on whether I'm using libc++ or libstdc++. #include <sstream> #include <iostream> int main() { int a = 0; void* optr = &a; void* iptr; std::stringstream ss; ss << optr; std::cout << ss.str() << 'n'; ss >> iptr; std::cout << iptr << 'n'; ret

使用libc ++和libstdc ++的void *类型的字符串流行为差异

以下测试程序根据我是使用libc ++还是libstdc ++返回不同的结果。 #include <sstream> #include <iostream> int main() { int a = 0; void* optr = &a; void* iptr; std::stringstream ss; ss << optr; std::cout << ss.str() << 'n'; ss >> iptr; std::cout << iptr << 'n'; return 0; } 我在OSX 10.9.2上使用Xcode 5以下版本

Are there equivalents to pread on different platforms?

I am writing a concurrent, persistent message queue in C++, which requires concurrent read access to a file without using memory mapped io. Short story is that several threads will need to read from different offsets of the file. Originally I had a file object that had typical read/write methods, and threads would acquire a mutex to call those methods. However, it so happened that I did not a

是否有相当于在不同平台上进行预编译?

我在C ++中编写了一个并发的持久消息队列,它需要对文件进行并发读取访问而不使用内存映射io。 简短的故事是,几个线程将需要从文件的不同偏移中读取。 最初我有一个具有典型读/写方法的文件对象,并且线程将获得一个互斥体来调用这些方法。 然而,恰巧我没有正确地获取互斥锁,导致一个线程在读/写期间移动文件偏移量,另一个线程开始读取/写入文件的错误部分。 所以,偏执的解决方案是每个线程都有一个打开的文件句柄。

c++: Split string by delimiter

This question already has an answer here: The most elegant way to iterate the words of a string [closed] 74 answers How do I tokenize a string in C++? 34 answers

c ++:按分隔符分割字符串

这个问题在这里已经有了答案: 迭代字符串最优雅的方式[已关闭] 74个答案 如何在C ++中标记字符串? 34个答案