#include <iostream>
#include <memory>
using namespace std;
int main () {
auto_ptr<int> p;
p.reset (new int);
*p=5;
cout << *p << endl;
p.reset (new int);
*p=10;
cout << *p << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在用C++编写一个智能指针实现,而且我在使用const-correctness方面遇到了一些麻烦.以下是代码的摘录:
template <class T> class Pointer {
T* pointee;
public:
Pointer(const Pointer<T>& other) { // must be const Pointer& for assignments
pointee = other.getPointee();
}
T* getPointee() const {
return pointee;
}
};
Run Code Online (Sandbox Code Playgroud)
这是一种方法,但是我感到不安的是const会员没有返回const指针.另一种可能性是getPointee()返回a const T*并const_cast<T*>在复制构造函数中执行a .
有没有更好的方法呢?如果没有,你认为哪个是较小的邪恶,返回一个非常数或做一个const_cast?
这里的类似问题似乎都使用了boost,我没有使用它.
我想要做的是通过以下方式证明:
在"所有者"中:
std::shared_ptr<State> m_state;
m_state = make_shared<State>(param);
m_state = m_state->SomeVirtualFunction(); // The original m_state object gets destroyed
Run Code Online (Sandbox Code Playgroud)
在"拥有":
std::shared_ptr<State> State::SomeVirtualFunction() {
return std:shared_ptr<State>(this);
}
Run Code Online (Sandbox Code Playgroud)
在MSVS 2012中的Visual C++中,拥有的对象被破坏.我该如何保持活力?
我刚刚开始使用C++并试图了解智能指针.显然,下面的代码会崩溃(我想这是因为赋值创建了shared_ptr的副本?).有没有办法foo.Other通过调用某种set方法来保持更新ptrs[3]?
class Foo
{
public:
int X;
std::shared_ptr<Foo> Other;
Foo() : X(10) { }
};
int main()
{
Foo foo;
std::vector<std::shared_ptr<Foo>> ptrs(10);
foo.Other = ptrs[3];
std::shared_ptr<Foo> other = std::shared_ptr<Foo>(new Foo());
ptrs[3] = other;
std::cout << foo.Other->X << std::endl; // throws Access violation exception
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我得到的异常,因为它指向null:
Test.exe中0x01219AEF处的第一次机会异常:0xC0000005:访问冲突读取位置0x00000000.Test.exe中0x01219AEF处的未处理异常:0xC0000005:访问冲突读取位置0x00000000.
做std::shared_ptr和std::unique_ptr功能.get()和operator->做的完全一样吗?
或者与std::vectors .at()和operator[]?有区别吗?
我正在尝试使用c ++智能指针反向打印一个char数组.我遇到两个问题.1是我正在尝试调试的运行时错误,另一个是每次我必须增加shared_ptr的事实我必须使用get()方法.
我正在粘贴我的两个功能.一个只使用指针反转字符串的人.和一个使用共享ptr.
int display_string_reversep(char* astring)
{
char* achar = astring;
if((*achar) != '\0')
{
char* x= achar;
x++;
display_string_reversep(x);
cout<<(*achar);
}
return 0;
}
int display_string_reverseup(shared_ptr<char> astring)
{
shared_ptr<char> achar(astring);
//if((*achar) != '\0')
if(achar != nullptr)
{
if(*(achar.get()+1) != '\0')
{
shared_ptr<char> x(achar.get()+1);
//x++;
display_string_reverseup(x);
}
cout<<(achar);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我是c ++ 11的新手,这只是我自己完成的一个小练习.到目前为止,互联网没有让我增加共享指针的其他方法.在那儿 ?
char astring [] = {'F','e','l','l','o','w','\0'};
display_string_reversep(astring);
display_string_reverseup(shared_ptr<char>(astring));
Run Code Online (Sandbox Code Playgroud) 我想传递一个QWidget指针参数(plot2)到Qt的addWidget(QWidget * T,...)功能,需要一个指向一个QWidget作为第一个参数.如果我传递原始指针plot2,我按预期得到两个并排面板.
原始指针版本
plot1 = new QWidget;
plot2 = new QWidget;
gridLayout = new QGridLayout(gridLayoutWidget);
...
gridLayout->addWidget(plot1, 1, 1, 1, 1);
gridLayout->addWidget(plot2.get(), 1, 2, 1, 1);
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用std::unique_ptr版本plot2并通过std :: unique_ptr(plot2)传递指针,如下面的代码片段所示,则相关的面板plot2会丢失,而编译器不会提出任何投诉.
智能指针版
plot1 = new QWidget;
auto plot2 = std::unique_ptr<QWidget>(new QWidget);
gridLayout = new QGridLayout(gridLayoutWidget);
...
gridLayout->addWidget(plot1, 1, 1, 1, 1);
gridLayout->addWidget(plot2.get(), 1, 2, 1, 1); // this panel goes missing
Run Code Online (Sandbox Code Playgroud)
我尝试使用过std::shared_ptr但结果仍然相同.
什么的作品当然是,如果我 …
有很多事情需要说.首先,我想知道下面的方法是否被认为是设计模式甚至是一种常用技术(这就是为什么我没有提供有关标题的更多信息).如果是这样,那么名字是什么?无论如何,这是我想要实现的缩小版本.由于我需要使用复制,我发现使用std :: shared_ptr是最好避免被释放(删除)的指针.
class Foo
{
public:
Foo() : ptr(nullptr) {}
Foo(const Foo& foo) : ptr(foo.ptr) {}
virtual ~Foo() = default;
void whatever() {
if (ptr)
ptr->whateverHandler();
}
void reset() {
ptr.reset();
}
void resetBar() {
ptr.reset(new Bar);
}
// Other resets here...
protected:
Foo(Foo* foo) : ptr(foo) {}
private:
// Every child class should override this
virtual void whateverHandler() {
throw "whateverHandler cant be called within base class";
}
protected:
std::shared_ptr<Foo> ptr;
};
class Bar : public Foo
{ …Run Code Online (Sandbox Code Playgroud) 我正在处理一个树结构,它具有unique pointers作为类成员的向量.在研究它时,我意识到如果唯一指针所拥有的对象作为参数,我不确定如何将唯一指针初始化为类成员.
所以,我只是问:如果将拥有的对象作为参数,我应该如何将唯一指针初始化为类成员?
我应该使用new(addTheObject(new Object()))然后使用std::make_unique吗?我应该将对象作为unique_ptr(addTheObject(std::unique_ptr<Object>& theObject))传递并使用std::move(theObject)吗?
处理这个问题的正确方法是什么?
如果你需要更具体的例子:
我有一个DT_Node类,它是树的一个节点,我将使用构造一个树DT_Node.
DT_Node有一个被调用的方法addChild()用于将子元素插入其节点向量中.
DT_Node将在不同的cpp文件中用于构造树,这意味着另一个cpp文件将使用addChild()方法来添加节点的子节点.
// DT_Node.hpp
class DT_Node
{
public:
DT_Node();
virtual ~DT_Node();
virtual void decide() = 0;
virtual void addChild( ??? );
private:
std::vector< std::unique_ptr<DT_Node> > mNodes;
};
Run Code Online (Sandbox Code Playgroud)
// DT_Node.cpp
DT_Node::DT_Node()
{
}
DT_Node::~DT_Node()
{
mNodes.clear();
}
void DT_Node::addChild( ??? )
{
???
}
Run Code Online (Sandbox Code Playgroud) 我想知道以下代码是否是C++中的一个好模式?
完全没有问题.代码有效.但我想知道这是否会导致某种问题.
#include <iostream>
#include <memory>
template <typename T>
class Class {
public:
std::shared_ptr<Class> shared_ptr;
Class() : shared_ptr(this) {}
~Class() { shared_ptr.reset(); }
};
Run Code Online (Sandbox Code Playgroud) c++ ×10
smart-pointers ×10
c++11 ×4
shared-ptr ×2
stl ×2
auto-ptr ×1
const ×1
inheritance ×1
operators ×1
pointers ×1
qt ×1
recursion ×1
unique-ptr ×1