我可以访问null shared_ptr对象的成员函数:
#include <memory>
#include <iostream>
class A
{
public:
int getNum() {return 1234;}
};
int main()
{
std::shared_ptr<A> pA(nullptr);
std::cout << pA->getNum() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当我期待一个例外时返回1234.同样的结果发生了
std::shared_ptr<A> pA();
Run Code Online (Sandbox Code Playgroud)
要么
std::shared_ptr<A> pA();
pA.reset();
Run Code Online (Sandbox Code Playgroud)
这真的是预期的行为吗?如果是corect shared_ptr defination在成员函数调用的情况下抛出异常是什么?
使用VS2010.
我有一些逻辑,我将std :: shared_ptrs用于继承层次结构中的对象.有一点我需要根据它们的真实类型处理这些对象,所以我使用双重调度(即我在基类上调用一个方法,然后在另一个对象上调用一个方法,实际类型,参见eg GoF中的访客模式).
现在,此时我可以使用正确的类型或副本传递对象的引用.由于几个原因,副本是不可能的.引用通常很好,因为调用发生在shared_ptr所在的范围之下,因此在调用发生时不会销毁它.但是对于某些子类型,我需要将对象存储在STL容器中,因此我需要绝对确定它不会被销毁.显然,裸指针或新的shared_ptrs在这里不起作用,所以我需要获得对其调用的shared_ptr的引用.
现在我正在做以下事情:我使用命名构造函数而不是真实构造函数来创建对象.这会在对象内部设置weak_ptr,并为该对象的使用提供shared_ptr.当双回调发生时,我从weak_ptr获取一个新的shared_ptr并将其存储在Container中,因此该对象不会被销毁.然而,这使我的建筑逻辑真的很难看.
有没有更好的方法来做到这一点?
我有以下代码:
#include <memory>
int main(void)
{
std::shared_ptr<int> currInt(nullptr);
std::shared_ptr<int> newInt(new int);
currInt = newInt;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
事实证明,这不是有效的C++ 11(它用于草稿版本),并且赋值构造函数现在使用移动语义.我不明白的东西.
有人可以解释我是如何修改上面的代码使它...工作?
我在C++ 11中使用std :: shared_ptr,我想了解以这种方式分配类型为T的结构是否更好:
T a_data;
std::shared_ptr<T> my_pointer(new T);
*my_pointer = a_data;
Run Code Online (Sandbox Code Playgroud)
或者喜欢:
memcpy(&my_pointer, data, sizeof(T));
Run Code Online (Sandbox Code Playgroud)
或者喜欢:
my_pointer.reset(a_data);
Run Code Online (Sandbox Code Playgroud)
问候
麦克风
我试图第一次使用共享指针,我遇到了一个问题,我无法从指针访问Element的成员函数.我已经看过网络上的例子了,似乎应该可以访问Element的功能,但我怀疑我在设置指针时做错了什么,但我无法解决问题.
如何从shared_ptr访问Element中的公共成员函数?(我正在使用Xcode 5.1.1)
#include <memory>
#include <iostream>
class Element
{
private:
std::string name;
std::shared_ptr<Element> * firstChild;
std::shared_ptr<Element> * lastChild;
std::shared_ptr<Element> * nextSibling;
public:
void addChild(std::shared_ptr<Element> * child)
{
if (lastChild != nullptr) {
lastChild->setNextSibling(child); //Error: No member named 'setNextSibling' in 'std::__1::shared_ptr<Element>'
lastChild = child;
}
else {
firstChild = lastChild = child;
}
}
void setNextSibling(std::shared_ptr<Element>* p)
{
nextSibling = p;
}
};
Run Code Online (Sandbox Code Playgroud) 使用Normal指针,它很简单:
int* p = new int;
int* x = new int;
p=x;
Run Code Online (Sandbox Code Playgroud)
但是有了共享,有:swap,reset等等
std::shared_ptr<int> x = NULL;
std::shared_ptr<int> y = NULL;
Run Code Online (Sandbox Code Playgroud)
我知道重置用于"新"
x.reset(new int(5));
Run Code Online (Sandbox Code Playgroud)
如果我希望x和y都指向这个新的整数5,我是否使用reset或swap或=?我真的很困惑.
y = x;
y.swap(x);
y.reset(x);
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下吗?
谢谢.
我试图将自定义deletor传递给std :: shared_ptr时遇到奇怪的错误:
std::shared_ptr<unsigned char*> SDLWindow::drawGrid(const Grid* grid) {
SDL_Surface* rgbSurface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGB888, 0);
//...error checking and locking the SDL_Surface, omitted for brevity
unsigned char* pixelsPtr = (unsigned char*)(rgbSurface->pixels);
//need a custom deleter because we created a copy of the SDL_Surface
//we cant directly delete the pixel data but need to delete the underlying SDL_Surface instead
auto surfaceDeleter = [rgbSurface](decltype(pixelsPtr)* ignored)
//don't directly delete the pixel buffer, delete the underlying SDL_Surface instead
{
//unlock the surface if necessary
if(SDL_MUSTLOCK(rgbSurface)) …Run Code Online (Sandbox Code Playgroud) 我知道如果我必须在lambda中调用成员函数进行回调,我在lambda中捕获它并且它运行良好.但是我最近看到了一些崩溃,看来成员函数在被指向的对象被破坏后才被访问.简而言之,在关闭时,对象被销毁,但指针在lambda中传递,被访问并导致崩溃.
所以,我试图了解社区在这种情况下通常会做些什么.我找不到这个,但我认为shared_ptr可能是一个选择.
任何建议/线索将不胜感激,以帮助我理解和实施替代方案.
我有这个向量的指针向量:
std::vector<std::shared_ptr<std::vector<int>>> vec;
Run Code Online (Sandbox Code Playgroud)
我想初始化第一个指针作为指向空向量的指针.我试过这个:
vec.push_back(nullptr);
Run Code Online (Sandbox Code Playgroud)
然后我想将一些值push_back到nullptr指向的向量.我这样做了:
vec[0]->push_back(x);
Run Code Online (Sandbox Code Playgroud)
我知道这是错误的,它最终会出现在段错误中.我尝试用make_shared()而不是nullptr做一些事情,但我仍然无法弄清楚如何实现我提到的.谁能帮我解决这个例子?
当我写作
std::shared_ptr<MyType> x;
Run Code Online (Sandbox Code Playgroud)
使用指向nullptr的空共享指针初始化x.但我希望它能自动调用MyType的默认(或其他一些指定的构造函数).我知道,我可以写:
std::shared_ptr<MyType> x = std::maked_shared<MyTYpe>();
Run Code Online (Sandbox Code Playgroud)
但有时人们会忘记然后你遇到麻烦,所以编译器强制执行此操作会很好.