如何为堆栈分配的对象调用shared_from_this?在基类列表中的enable_shared_from_this是派生类的用户的指示器,用于仅在堆上创建它(我们只是希望正确的类使用)或者我们可以对这些错误有更强的保护吗?或者我不明白一些时刻?
示例代码:
__CODE__返回COUNT个查询?
那么.. foo的精确运行时类型是什么?
class C : public enable_shared_from_this<C>
{
public:
shared_ptr<C> method() { shared_from_this(); }
};
void func()
{
C c;
shared_ptr<C> ptr = c.method(); // exception comming from shared_from_this()
}
Run Code Online (Sandbox Code Playgroud) 假设我有类似的课程
class A{
public:
A(int a, boost::shared_ptr<int> ptr){
// whatever!
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是,该ptr的默认值是多少?我希望能够使用创建该类的实例
A myA(5);
Run Code Online (Sandbox Code Playgroud)
当然我知道我可以用一个参数创建另一个构造函数,但我正在寻找类似的东西
A(int a, boost::shared_ptr<int> ptr = WAT?)
Run Code Online (Sandbox Code Playgroud)
可能吗?目前我正在使用双构造方式,但这样做会很棒.
std::unique_ptr<int> ptr;
ptr = new int[3]; // error
Run Code Online (Sandbox Code Playgroud)
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int *' (or there is no acceptable conversion)
为什么这不编译?如何将本机指针附加到现有的unique_ptr实例?
避免使用未命名的shared_ptr临时值来保存输入; 要了解为什么这是危险的,请考虑以下示例:
void f(shared_ptr<int>, int);
int g();
void ok() {
shared_ptr<int> p(new int(2));
f(p, g());
}
void bad() {
f(shared_ptr<int>(new int(2)), g());
}
Run Code Online (Sandbox Code Playgroud)
函数ok遵循字母的准则,而bad构造临时shared_ptr,承认内存泄漏的可能性.由于函数参数是以未指定的顺序计算的,因此可以首先计算new int(2),g()second,如果g抛出异常,我们可能永远不会访问shared_ptr构造函数.<...>
通过使用boost/make_shared.hpp中定义的make_shared或allocate_shared工厂函数,也可以消除上述异常安全问题.这些工厂功能还通过合并分配提供了效率优势.
我想我会开始使用make_shared,但我想知道这一点建议是否仍然适用于C++ 11 shared_ptr.我问,因为我并不完全理解为什么投掷g()会阻止ctor被调用.
有时我真的很确定我想要循环依赖指针,并且循环中的每个对象都应该能够使用他的指针(所以它不能是weak_ptr).
我的问题是:这是否意味着我的设计不好?
如果我想实现图形怎么办?我可以使用智能指针吗?在图表中有循环,但使用weak_ptr我不能使用" - >".我能做什么?
我在StackOverflow上阅读了一些文章,参考和主题,但看起来我仍然没有得到智能指针.真的,为什么不存在一些带有" - >"的weak_ptr变体?
当我尝试以下操作时,GCC编译器会抱怨(见下文).class Face需要不完整,因为它包含指向class Element同样包含指针的指针class Face.换句话说,类之间存在循环依赖关系.我该如何解决?
错误:'sizeof'无效应用于不完整类型'Face'
class Face; // needs to be incomplete
class Element
{
std::vector < std::unique_ptr <Face> > face;
};
class Face
{
std::vector < std::unique_ptr <Element> > elm;
};
Run Code Online (Sandbox Code Playgroud) 我有以下代码来测试智能指针作为键std::map,我在Mac和Linux上运行代码,但我观察到不同的输出,是一个错误还是我做错了什么?
#include <iostream>
#include <memory>
#include <string>
#include <map>
using namespace std;
class Dog {
public:
typedef shared_ptr<Dog> sptr;
Dog(const string &name) : name_(name) { }
friend bool operator<(const Dog &lhs, const Dog &rhs) {
cout << "Dog::operator< called" << endl;
return lhs.name_ < rhs.name_;
}
friend bool operator<(const sptr &lhs, const sptr &rhs) {
cout << "Dog::operator< sptr called" << endl;
return lhs->name_ < rhs->name_;
}
private:
string name_;
};
void test_raw_object_as_map_key() {
cout << "raw object …Run Code Online (Sandbox Code Playgroud) 在以下代码中:
智能指针数据成员pImpl(类Impl)和原始指针pc(类CAT)都是不完整的数据类型,Widget.h中没有这两个类的定义
//widget.h
#ifndef W_H_
#define W_H_
#include <memory>
class Widget {
public:
Widget();
~Widget() { //
delete pc; // I know I should put ~Widget to .cpp
// I just want to show the difference in behavior
// between raw pointer and smart pointer(both has incomplete type)
// when widget object destructs
}
private:
struct Impl;
std::shared_ptr<Impl> pImpl; // use smart pointer
struct CAT;
CAT *pc; //raw pointer
};
#endif
Run Code Online (Sandbox Code Playgroud)
//widget.cpp
#include "widget.h"
#include <string>
#include <vector>
#include <iostream> …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查a std::shared_ptr是否为空.这样做有什么区别
std::shared_ptr<int> p;
if (!p) { // method 1 }
if (p == nullptr) { // method 2 }
Run Code Online (Sandbox Code Playgroud) 我有2 shared_ptr秒定义和分配nullptr.在案例1中,我使用默认构造函数,在案例2中,我使用了构造函数和delete方法.
shared_ptr<int> sptr2(nullptr);
cout << "sptr2 use_count: " << sptr2.use_count() << endl;
shared_ptr<int> sptr6(nullptr, default_delete<int>());
cout << "sptr6 use_count: " << sptr6.use_count() << endl;
Run Code Online (Sandbox Code Playgroud)
输出是:
sptr2 use_count: 0
sptr6 use_count: 1
Run Code Online (Sandbox Code Playgroud)
我不明白为什么sptr6在没有任何有效指针时使用count为1.
g ++(GCC)4.8.5 20150623(Red Hat 4.8.5-16)
c++ ×10
smart-pointers ×10
c++11 ×7
shared-ptr ×5
boost ×2
containers ×1
map ×1
pointers ×1
weak-ptr ×1