在声明一个std::unique_ptr<std::string>但没有分配它之后(所以它包含一个std::nullptr开始) - 如何为它赋值(即我不再希望它保持std::nullptr)?我试过的方法都没有.
std::unique_ptr<std::string> my_str_ptr;
my_str_ptr = new std::string(another_str_var); // compiler error
*my_str_ptr = another_str_var; // runtime error
在哪里another_str_var是std::string先前声明和分配的.
很明显,我对std::unique_ptr正在做的事情的理解非常不足......
我试图创建和使用make_unique的std::unique_ptr,以同样的方式std::make_shared对存在std::shared_ptr 这里描述.Herb Sutter 提到了可能的实现make_unique,如下所示:
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}
它似乎对我不起作用.我正在使用以下示例程序:
// testproject.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <memory>
#include <utility>
struct A {
  A(int&& n) { std::cout << "rvalue overload, n=" << n << "\n"; }
  A(int& n)  { std::cout << "lvalue overload, n=" << n << …使用安全吗?
vector.emplace_back( new MyPointer() );
或者,向量中抛出异常或某些失败会导致内存泄漏吗?
是否更好地执行以下某种形式,首先将指针放在临时的unique_ptr中.
vector.emplace_back( std::unique_ptr<MyPointer>( new MyPointer() ) );
因此,如果发生向量故障,临时unique_ptr仍将清理内存?
我有一个简单的类,有一个看起来像这样的构造函数:
Event(std::function<void()> &&f) : m_f(std::move(f)) { }
构造函数可以与std :: bind一起使用:
Thing thing;
std::unique_ptr<Event> ev(new Event(std::bind(some_func,thing)));
以上述方式使用它会导致'thing'的一个副本构造,然后在该副本上移动构造.
但是,执行以下操作:
std::unique_ptr<Event> ev = make_unique<Event>(std::bind(some_func,thing));
结果有两个移动结构.我的问题是:
这是最小的例子:
#include <iostream>
#include <memory>
#include <functional>
using namespace std;
class Thing
{
public:
    Thing() : x(0)
    {
    }
    Thing(Thing const &other)
    {
        this->x = other.x;
        std::cout << "Copy constructed Thing!\n";
    }
    Thing(Thing &&other)
    {
        this->x = other.x;
        std::cout << "Move constructed Thing!\n";
    }
    Thing & operator = (Thing const &other)
    {
        this->x = other.x;
        std::cout …在最近实施零规则主题下的超载期刊中,作者描述了我们如何避免编写五个操作符规则,因为编写它们的原因是:
这两个都可以通过使用智能指针来处理.
在这里,我对第二部分特别感兴趣.
请考虑以下代码段:
class Base
{
public:
    virtual void Fun() = 0;
};
class Derived : public Base
{
public:
    ~Derived()
    {
        cout << "Derived::~Derived\n";
    }
    void Fun()
    {
        cout << "Derived::Fun\n";
    }
};
int main()
{
    shared_ptr<Base> pB = make_shared<Derived>();
    pB->Fun();
}
在这种情况下,正如文章的作者解释的那样,我们通过使用共享指针获得多态删除,这确实有效.
但是,如果我shared_ptr用a 替换unique_ptr,我不再能够观察到多态删除.
现在我的问题是,为什么这两种行为有所不同?为什么不shared_ptr照顾多态删除unique_ptr?
我在c ++ 11中有一个关于智能指针的问题.我已经开始研究C++ 11(我通常在c#中编程)并阅读有关智能指针的一些内容.现在我有了问题,智能指针是否完全取代了"旧"指针的风格,我应该总是使用它们吗?
在unique_ptr似乎解决在C++内存管理的所有问题,还是我错了?
例如:
std::unique_ptr<GameManager> game (new GameManager());
game->Start();
似乎比以下更聪明:
auto *game2 = new GameManager();
game2->Start();
delete game2;
谢谢,我有点困惑!
我需要std::unique_ptr从一个具有构造函数的类中创建一个采用一个参数的类。我找不到有关如何执行此操作的参考。这是无法编译的代码示例:
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
class MyClass {
    public:
        MyClass(std::string name);
        virtual ~MyClass();
    private: 
        std::string myName;
};
MyClass::MyClass(std::string name) : myName(name) {}
MyClass::~MyClass() {}
class OtherClass {
    public:
        OtherClass();
        virtual ~OtherClass();
        void MyFunction(std::string data);
        std::unique_ptr<MyClass> theClassPtr;
};
OtherClass::OtherClass() {}
OtherClass::~OtherClass() {}
void OtherClass::MyFunction(std::string data)
{
    std::unique_ptr<MyClass> test(data); <---------- PROBLEM HERE!
    theClassPtr = std::move(test);
}
int main()
{
    OtherClass test;
    test.MyFunction("This is a test");
}
错误与我初始化std::unique_ptr代码中指出的方式有关。
原始代码和错误可以在这里找到。
感谢您帮助我解决该问题。
我想知道newC#中是否有类似C++ 的声明
C#允许你这样做,它只是稍微加密了代码:
FuncCall( new Foo() {
    Bar = "sausage",
    Boo = 4
} );
我只是觉得这在C++中有点草率:
unique_ptr<Foo> foo( new Foo() );
foo.Bar = "sausage";
foo.Boo = 4;
FuncCall( move( foo ) );
Foo可能看起来像这样:
class Foo
{
public:
    Foo();
    string Bar;
    int Boo;
}
为什么我不只是将所有内容放入构造参数中?
因为当你必须这么多时,这是愚蠢的:
Foo( int width, int height, string title, string className, string thjis, stihjrjoifger gfirejgoirejgioerjgoire ) 它一直在继续......虽然我已经拥有了我班级内的属性......所以只是想知道它是否可以完成.. 
我知道问题的标题看起来有点令人头疼,但我真的不知道怎么用一句话来问这个问题.我只会告诉你我的意思:
void f(T *obj)
{
    // bla bla
}
void main()
{
    f(new T());
}
据我所知,(几乎)每个新的都需要一个删除,这需要一个指针(由new返回).在这种情况下,new返回的指针不存储在任何地方.这会是内存泄漏吗?
C++是否有某种神奇的功能(程序员看不见),它会在函数结束后删除对象,或者这种做法总是一个坏主意?
我注意到我正在编译的某个库中的某个行为,这有点出乎意料,并想澄清一下.
有一个类有以下形式的方法:
void notify(Frame & frame);
现在,有一个调用者使用unique_ptr如下:
std::unique_ptr <Frame> localFrame (new Frame(rows, cols));
现在当它调用方法时:
obj->notify(*localFrame);
所以这依赖于底层指针到引用的隐式转换.
我的问题是,这是跨平台和预期的行为吗?我有什么用途可以做类似的事情:
obj->notify(*localFrame->get());
c++ ×10
c++11 ×6
unique-ptr ×4
c++14 ×2
pointers ×2
function ×1
memory-leaks ×1
polymorphism ×1
rule-of-zero ×1
stdvector ×1
string ×1
visual-c++ ×1