- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
以下...
class TestClass
{
public:
TestClass(const char* szParam, int nParam)
: m_strParam(szParam)
, m_nParam(nParam)
{
Dbg_Printf("2 param constructor - %s, %d\n", m_strParam.c_str(), m_nParam);
}
TestClass()
: m_strParam("Default")
, m_nParam(0)
{
Dbg_Printf("0 param constructor - %s, %d\n", m_strParam.c_str(), m_nParam);
}
virtual ~TestClass()
{
Dbg_Printf("Destructor - %s, %d\n", m_strParam.c_str(), m_nParam);
m_strParam.clear();
m_nParam = 0;
}
std::string m_strParam;
int m_nParam;
};
void Test()
{
Dbg_Printf("Start\n");
{
Dbg_Printf("Allocating 0 param TestClass\n");
auto pTest_0Params = std::make_shared<TestClass>();
Dbg_Printf("Done allocating, going out of scope\n");
}
{
Dbg_Printf("Allocating 2 param TestClass\n");
const char* szTest = "2 param";
int foo = 2;
auto pTest_2Params = std::make_shared<TestClass>(szTest, foo);
Dbg_Printf("Done allocating, going out of scope\n");
}
Dbg_Printf("Done\n");
}
产生以下结果
Start
Allocating 0 param TestClass
0 param constructor - Default, 0
Done allocating, going out of scope
Destructor - Default, 0
Allocating 2 param TestClass
2 param constructor - 2 param, 2
Destructor - 2 param, 2
Destructor - 2 param, 2
Done allocating, going out of scope
Destructor - 2 param, 2
Done
对 make_shared 的 2 参数调用最终在分配和赋值时调用析构函数两次。
我已经用析构函数中的断点一次又一次地调试了。它在 2 参数情况下的 make_shared 调用的中间。
看不到代码有任何明显的错误。还有其他人看到这个吗?想法?
最佳答案
你有指定-std=c++11
吗?
当我在桌面上编译时:
clang++ -std=c++03 -stdlib=libc++ test.cpp
我得到你得到的:
Start
Allocating 0 param TestClass
0 param constructor - Default, 0
Done allocating, going out of scope
Destructor - Default, 0
Allocating 2 param TestClass
2 param constructor - 2 param, 2
Destructor - 2 param, 2
Destructor - 2 param, 2
Done allocating, going out of scope
Destructor - 2 param, 2
Done
但是当我打开 -std=c++11
我得到:
Start
Allocating 0 param TestClass
0 param constructor - Default, 0
Done allocating, going out of scope
Destructor - Default, 0
Allocating 2 param TestClass
2 param constructor - 2 param, 2
Done allocating, going out of scope
Destructor - 2 param, 2
Done
关于ios - iOS 中 std::make_shared 调用析构函数的可变版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267881/
我在 boost::asio 中做了一个 echo-server 示例。但是使用 boost::make_shard 会导致“未知异常”,而 std::make_shared 则不会。请参阅注释行。
我一直在做一些现场性能测试 1>std::shared_ptr, std::make_shared based on 'gcc 4.7.2' & 'VC10 implementation' 2>boo
问题 1> Line 1 的使用率真的比 Line 2 好吗? boost::shared_ptr shpStr = boost::make_shared(); // Line 1 boost::sh
正如 Bjarne Stroustrup 的“C++ 之旅”中所述,作为一种已知的 C++14 实践,人们应该避免在代码中使用裸露的 new 和 delete。标准库提供 std::make_shar
这可能是重复的,但我无法在任何地方找到解决方案。 我有这样的源代码: struct Blob{ //... static void *operator new(size_t si
我有一个带有模板构造函数的类,并且想要一个 shared_pointer给它。如: class A; typedef std::shared_ptr A_ptr; class A { public:
将 VS 2015 与 v120 结合使用。 所以我得到了内存异常,因为当我在一个已经构造的对象上调用 make_shared() 时。已经构造的对象有一个指向另一个用 new 分配的对象的指针,所以
在this回答 T.C.州 boost::make_shared etc. support array types - either one of unknown size, or one of fi
虽然我有std::tr1::shared_ptr在我的编译器中可用,我不有make_shared . 谁能告诉我如何正确实现 make_shared ?我懂了我需要使用可变参数来为 T 的构造函数提供
我正在尝试自己实现 shared_ptr。 make_shared 有问题。 std::make_shared 的主要特点是它在连续的内存块中分配计数器 block 和对象。我怎样才能做同样的事情?
除非定义了 B0RKEN(就像命令行上的 -DB0RKEN 一样),否则编译以下内容: #include #include #include using boost::shared_ptr; u
有什么方法可以使用 make_shared 而不是 shared_ptr 作为抽象类型? 例子: #include #include class Foo { public: virtual
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我明白了: shared_ptr x = make_shared(); 效率高于: shared_ptr x(new X()); 而且我了解其中的优势。但是,我不明白为什么编译器不能有这样的规则 "i
使用 gcc 4.6.2,如果构造函数抛出异常,make_shared() 会给出无用的回溯(显然是由于某些重新抛出)。我正在使用 make_shared() 来节省一些输入,但这是显示停止器。我创建
从 C++11 开始,由于多种原因,开发人员倾向于将智能指针类用于动态生命周期对象。对于那些新的智能指针类、标准,甚至建议不要使用像 new 这样的运算符,而是建议使用 make_shared 或 m
从理论上讲,make_shared() 和shared_ptr 之间的一个区别是内存分配技术。 make_shared() 应该使用一个 block ,而 shared_ptr 应该使用两个 bloc
为了规避restriction on partially supplied explicit template arguments ,我将要从中推导出类模板参数 ( Internal ) 的结构嵌
为什么这个 make_shared 在两个单独的调用中分配相同的内存地址? typedef struct { int a; int b; int c; } test_t; vo
我最近正在处理 shared_ptr 的问题。我很好奇如果 make_shared 失败了,它会引发异常吗?是否存在 make_shared 返回 nullptr 但没有任何异常的情况? 最佳答案 来
我是一名优秀的程序员,十分优秀!