该enable_shared_from_this助手包含被创建共享指向对象时设置的弱指针.这意味着有一个引用计数(单独分配或与使用的对象一起分配make_shared)和一个额外weak_ptr的对象.
现在为什么不简单地包含引用计数呢?shared_ptr从哑指针设置时,必须完全定义类型,因此shared_ptr构造函数或赋值运算符可以检测从中派生的类型enable_shared_from_this并使用正确的计数器,并且格式可以保持不变,因此复制无关紧要.实际上,shared_ptr已经有必要检测它来设置嵌入式weak_ptr.
我有一个对象(Z),它来自另外两个对象(A和B).
A和B都从派生enable_shared_from_this<>分别enable_shared_from_this<A>和enable_shared_from_this<B>.
当然我打电话shared_from_this()给Z.当然编译器报告这个含糊不清.
我的问题是:
enable_shared_from_this<>还是会创建两个分开的引用计数(坏!)注意:当基类和派生类都继承自boost :: enable_shared_from_this但我没有真正回答时,我发现这个其他问题是坏的弱指针.我virtual也应该使用这个技巧吗?
我正在尝试重构我的代码,以便我使用前向声明而不是包含大量的标头.我是新手,对boost :: shared_ptr有疑问.
说我有以下界面:
#ifndef I_STARTER_H_
#define I_STARTER_H_
#include <boost/shared_ptr.hpp>
class IStarter
{
public:
virtual ~IStarter() {};
virtual operator()() = 0;
};
typedef boost::shared_ptr<IStarter> IStarterPtr;
#endif
Run Code Online (Sandbox Code Playgroud)
然后我在另一个类中有一个函数,它将一个IStarterPtr对象作为参数,比如说:
virtual void addStarter(IStarterPtr starter)
{
_starter = starter;
}
...
IStarterPtr _starter;
Run Code Online (Sandbox Code Playgroud)
如何在不包含IStarter.h的情况下转发声明IStarterPtr?
我正在使用C++ 98,如果这是相关的.
最近我发现shared_ptr没有指向成员运算符的指针->*.我创建了简单的例子:
template <typename Pointer, typename Function, typename... Args>
auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...))
{
return (p->*f)(args...);
}
struct A {
void g() { std::cout << "A::g()\n"; }
};
int main() {
A a;
invoke1(&a, &A::g); // works!!
std::shared_ptr<A> sa = std::make_shared<A>();
invoke1(sa, &A::g); // compile error!!
}
Run Code Online (Sandbox Code Playgroud)
Q1:为什么会这样?为什么shared_ptr没有这个运算符?
我添加了这样的运算符,shared_ptr示例开始工作:
template <typename T, typename Result>
auto operator ->* (std::shared_ptr<T> pointer, Result (T::*function)()) ->decltype(std::bind(function, pointer))
{
return std::bind(function, pointer);
}
template <typename …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading boost-bind shared-ptr pointer-to-member
有时我真的很确定我想要循环依赖指针,并且循环中的每个对象都应该能够使用他的指针(所以它不能是weak_ptr).
我的问题是:这是否意味着我的设计不好?
如果我想实现图形怎么办?我可以使用智能指针吗?在图表中有循环,但使用weak_ptr我不能使用" - >".我能做什么?
我在StackOverflow上阅读了一些文章,参考和主题,但看起来我仍然没有得到智能指针.真的,为什么不存在一些带有" - >"的weak_ptr变体?
使用libc ++我std::shared_ptr::make_shared()在公共部分找到静态成员函数.当我已经为特化定义了类型别名时,它非常方便std::shared_ptr:
using T = int;
using P = std::shared_ptr< T >;
auto p = P::make_shared(123); // <=> std::make_shared< T >(123)
static_assert(std::is_same< decltype(p), P >::value);
Run Code Online (Sandbox Code Playgroud)
我担心符合标准,因为物品(1,2)从可信赖的来源没有提及静态成员函数make_shared的std::shared_ptr.
目前使用该功能是不是很糟糕?为什么?
例如,有一个函数可以找到一个对象,如果找到了对象则返回shared_ptr,并且必须以某种方式指示没有找到对象.
std::vector<std::shared_ptr> Storage::objects;
std::shared_ptr<Object> Storage::findObject()
{
if (objects.find)
{
return objects[x];
}
else
{
return nullptr;
}
}
std::shared_ptr<Object> obj = Storage::findObject();
if (obj)
{
print("found");
}
else
{
print("not found");
}
Run Code Online (Sandbox Code Playgroud)
返回使用nullptr隐式初始化的shared_ptr是否正确?它会起作用,但可以这样做吗?或者我应该返回shared_ptr默认构造而不是?
怎么会是weak_ptr?检查空weak_ptr已被返回的正确方法是什么?by weak_ptr :: expired函数还是有其他方法吗?如果通过weak_ptr :: expired检查是唯一的方法那么我如何区分该函数返回空指针,或者对象刚被删除(多线程环境)?
对于以下代码段,它在方法中显示不同的引用计数.有人可以解释为什么这些价值不同吗?
class Foo {
};
void f1( const std::shared_ptr<Foo>& ptr ) {
std::cout << "f1(): counts: " << ptr.use_count() << std::endl;
}
void f2( const std::shared_ptr<const Foo>& ptr ) {
std::cout << "f2(): counts: " << ptr.use_count() << std::endl;
}
int main() {
std::shared_ptr<Foo> ptr( new Foo );
std::cout << "main(): counts: " << ptr.use_count() << std::endl;
f1( ptr );
f2( ptr );
std::cout << "main(): counts: " << ptr.use_count() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
相应的输出:
main(): counts: 1
f1(): …Run Code Online (Sandbox Code Playgroud) 我有一个std::shared_ptr自定义删除器和删除器,我想采取原始的临时副本std::shared_ptr.以代码形式表示:
struct Foo : public std::enable_shared_from_this<Foo>
{};
void deleter(Foo *f)
{
{
std::shared_ptr<Foo> tmp = f->shared_from_this(); // Line A
}
delete f;
}
int main()
{
std::shared_ptr<Foo> foo(new Foo, &deleter);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在A线上,可以说一下这个电话shared_from_this()吗?这合法吗?如果是这样,标准是否对其返回值有所说明?如果我们enable_shared_from_this用不同的weak_ptr或全局的引用替换foo,那么答案是否相同?
锵用的libc ++和GCC用的libstdc ++都产生代码终止对bad_weak_ptr例外,但我似乎无法为跟踪这一要求的标准.这是特定于实现的,还是我错过了规则?
我找到的所有相关规则(引用C++ 11):
20.7.2.2.2
shared_ptr析构函数1 ...如果
*this拥有一个对象p和一个删除器d,d(p)则称为
2 [ 注意: ......由于销毁会*this减少与*this一个共享所有权的实例数量,在 …
如果共享指针要被复制到成员变量,是否应该通过引用或值作为参数传递给类?
复制共享指针将增加参考计数,我不想做任何不必要的副本,因此ref计数增量.将共享指针作为参考传递解决这个问题?我认为确实如此,但是还有其他问题吗?
价值传递:
class Boo {
public:
Boo() { }
};
class Foo {
public:
Foo(std::shared_ptr<Boo> boo)
: m_Boo(boo) {}
private:
std::shared_ptr<Boo> m_Boo;
};
int main() {
std::shared_ptr<Boo> boo = std::make_shared<Boo>();
Foo foo(boo);
}
Run Code Online (Sandbox Code Playgroud)
通过参考:
class Boo {
public:
Boo() { }
};
class Foo {
public:
Foo(std::shared_ptr<Boo>& boo)
: m_Boo(boo) {}
private:
std::shared_ptr<Boo> m_Boo;
};
int main() {
std::shared_ptr<Boo> boo = std::make_shared<Boo>();
Foo foo(boo);
}
Run Code Online (Sandbox Code Playgroud) c++ ×10
shared-ptr ×10
c++11 ×4
weak-ptr ×3
boost-bind ×1
c++14 ×1
c++98 ×1
destructor ×1
stl ×1
templates ×1