如何为堆栈分配的对象调用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) Glib :: RefPtr允许通过' ->'但不通过' *' 解除引用.为什么是这样?
我当然可以这样做:
class Foo {};
Glib::RefPtr<Foo> fooPtr;
fooPtr.operator->();
Run Code Online (Sandbox Code Playgroud)
文档特别提到他们离开了operator*().但他们没有提供任何指导原因.
为清晰起见,编写了示例:
我已经看到它认为" 你永远不需要取消引用 "一个RefPtr,但IMO似乎是虚假的,因为任何想要与动态和堆栈分配的对象一起使用的函数都需要最低的公分母接口,即pass-by -参考.
举个例子,例如:
struct Foo
{
void print() { printf( "Success" ); }
};
void myFunc( const Foo & foo ) { foo.print(); }
int main()
{
Foo foo0;
Glib::RefPtr<Foo> foo1Ptr( new Foo );
myFunc( foo0 );
myFunc( *foo1Ptr ); // error, no operator*()
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么这个位置是由Glib团队采取的?
有时我真的很确定我想要循环依赖指针,并且循环中的每个对象都应该能够使用他的指针(所以它不能是weak_ptr).
我的问题是:这是否意味着我的设计不好?
如果我想实现图形怎么办?我可以使用智能指针吗?在图表中有循环,但使用weak_ptr我不能使用" - >".我能做什么?
我在StackOverflow上阅读了一些文章,参考和主题,但看起来我仍然没有得到智能指针.真的,为什么不存在一些带有" - >"的weak_ptr变体?
我有以下代码来测试智能指针作为键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) 我有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 ++标准中使用智能指针.但是我没有掌握shared_from_this函数的用法.这是我有的:
#include <iostream>
#include <memory>
class CVerboseBornAndDie2 : public std::enable_shared_from_this<CVerboseBornAndDie2>
{
public:
std::string m_Name;
CVerboseBornAndDie2(std::string name) : m_Name(name)
{
std::cout << m_Name << " (" << this << ") is born!" << std::endl;
}
virtual ~CVerboseBornAndDie2()
{
std::cout << m_Name << " (" << this << ") is dying!" << std::endl;
}
};
int main(){
CVerboseBornAndDie2* vbad = new CVerboseBornAndDie2("foo");
std::shared_ptr<CVerboseBornAndDie2> p = vbad->shared_from_this();
}
Run Code Online (Sandbox Code Playgroud)
它会在行中抛出一个std :: bad_weak_ptr异常
std::shared_ptr<CVerboseBornAndDie2> p = vbad->shared_from_this();
Run Code Online (Sandbox Code Playgroud)
如果我反而这样做
std::shared_ptr<CVerboseBornAndDie2> p(vbad);
Run Code Online (Sandbox Code Playgroud)
它有效,我可以事后做
std::shared_ptr<CVerboseBornAndDie2> p2 = …Run Code Online (Sandbox Code Playgroud) 可能重复:
什么是智能指针,什么时候应该使用?
我正在阅读一篇文章,我找到了一个小例子来证明使用boost::scoped_ptr<T>:
#include <cstdlib>
#include <iostream>
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
static int count = 0;
class printer
{
int m_id;
public:
printer(void) :
m_id(count++)
{
}
~printer(void)
{
std::cout << "Printer " << m_id
<< " destroyed" << std::endl;
}
};
int
main(void)
{
boost::scoped_ptr<printer> p1(new printer);
boost::scoped_ptr<printer> p2(new printer);
std::cout << "Exiting test program" << std::endl;
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我在文章中唯一不理解的是这句话:
使用时
scoped_ptr,表示不打算或不允许所有权转移.
也许作为这个主题的初学者开始是错误的文章,但上述内容究竟意味着什么呢?
如何在智能指针中同时使用标量和数组?
使用new和delete指针的旧方法:
int *p;
if (useScalar) {
p = new int;
} else {
p = new int[10];
}
if (useScalar) {
delete p;
} else {
delete[] p;
}
Run Code Online (Sandbox Code Playgroud)
在智能指针中,我必须为每个标量和数组指针使用2个指针:
std::unique_ptr<int> p1(new int);
std::unique_ptr<int[]> p2(new int[10]);
Run Code Online (Sandbox Code Playgroud)
如何减少仅使用1个智能指针?
(假设我正在使用一个需要使用原始指针的库或框架,)
使用拥有一些数据的智能指针,然后将解除保护的智能指针的地址传递给需要原始指针的函数是否有效?
c++ ×10
smart-pointers ×10
c++11 ×5
shared-ptr ×4
boost ×1
glib ×1
gtk ×1
gtkmm ×1
map ×1
weak-ptr ×1