相关疑难解决方法(0)

如何将字符串的值赋给std :: unique_ptr <std :: string>?

在声明一个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
Run Code Online (Sandbox Code Playgroud)

在哪里another_str_varstd::string先前声明和分配的.

很明显,我对std::unique_ptr正在做的事情的理解非常不足......

c++ string unique-ptr

10
推荐指数
1
解决办法
6672
查看次数

make_unique不编译

我试图创建和使用make_uniquestd::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)... ) );
}
Run Code Online (Sandbox Code Playgroud)

它似乎对我不起作用.我正在使用以下示例程序:

// 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 << …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio-2010 visual-c++ variadic-templates c++11

9
推荐指数
2
解决办法
1万
查看次数

安全使用vector.emplace_back(new MyPointer); 矢量内部的故障会导致内存泄露吗?

使用安全吗?

vector.emplace_back( new MyPointer() );
Run Code Online (Sandbox Code Playgroud)

或者,向量中抛出异常或某些失败会导致内存泄漏吗?

是否更好地执行以下某种形式,首先将指针放在临时的unique_ptr中.

vector.emplace_back( std::unique_ptr<MyPointer>( new MyPointer() ) );
Run Code Online (Sandbox Code Playgroud)

因此,如果发生向量故障,临时unique_ptr仍将清理内存?

c++ memory-leaks stdvector c++11

9
推荐指数
1
解决办法
2904
查看次数

为什么make_unique使用可以将std :: bind作为参数的构造函数进行额外的移动?

我有一个简单的类,有一个看起来像这样的构造函数:

Event(std::function<void()> &&f) : m_f(std::move(f)) { }
Run Code Online (Sandbox Code Playgroud)

构造函数可以与std :: bind一起使用:

Thing thing;
std::unique_ptr<Event> ev(new Event(std::bind(some_func,thing)));
Run Code Online (Sandbox Code Playgroud)

以上述方式使用它会导致'thing'的一个副本构造,然后在该副本上移动构造.

但是,执行以下操作:

std::unique_ptr<Event> ev = make_unique<Event>(std::bind(some_func,thing));
Run Code Online (Sandbox Code Playgroud)

结果有两个移动结构.我的问题是:

  • 什么时候调用'thing'上的移动构造函数
  • 为什么用make_unique调用它两次?

这是最小的例子:

#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 …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

8
推荐指数
1
解决办法
1536
查看次数

C++ Zero of Zero:多态删除和unique_ptr行为

在最近实施零规则主题下的超载期刊中,作者描述了我们如何避免编写五个操作符规则,因为编写它们的原因是:

  1. 资源管理
  2. 多态删除

这两个都可以通过使用智能指针来处理.

在这里,我对第二部分特别感兴趣.

请考虑以下代码段:

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();
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,正如文章的作者解释的那样,我们通过使用共享指针获得多态删除,这确实有效.

但是,如果我shared_ptr用a 替换unique_ptr,我不再能够观察到多态删除.

现在我的问题是,为什么这两种行为有所不同?为什么不shared_ptr照顾多态删除unique_ptr

c++ polymorphism smart-pointers rule-of-zero

6
推荐指数
1
解决办法
2739
查看次数

C++ 11智能指针使用

我在c ++ 11中有一个关于智能指针的问题.我已经开始研究C++ 11(我通常在c#中编程)并阅读有关智能指针的一些内容.现在我有了问题,智能指针是否完全取代了"旧"指针的风格,我应该总是使用它们吗?

unique_ptr似乎解决在C++内存管理的所有问题,还是我错了?

例如:

std::unique_ptr<GameManager> game (new GameManager());

game->Start();
Run Code Online (Sandbox Code Playgroud)

似乎比以下更聪明:

auto *game2 = new GameManager();

game2->Start();

delete game2;
Run Code Online (Sandbox Code Playgroud)

谢谢,我有点困惑!

c++ pointers smart-pointers unique-ptr c++11

6
推荐指数
2
解决办法
4616
查看次数

C ++如何从采用构造函数参数的类中创建std :: unique_ptr

我需要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");
}
Run Code Online (Sandbox Code Playgroud)

错误与我初始化std::unique_ptr代码中指出的方式有关。

原始代码和错误可以在这里找到。

感谢您帮助我解决该问题。

c++ unique-ptr c++11

5
推荐指数
1
解决办法
1912
查看次数

是否有像C#中的C++新声明

我想知道newC#中是否有类似C++ 的声明

C#允许你这样做,它只是稍微加密了代码:

FuncCall( new Foo() {
    Bar = "sausage",
    Boo = 4
} );
Run Code Online (Sandbox Code Playgroud)

我只是觉得这在C++中有点草率:

unique_ptr<Foo> foo( new Foo() );
foo.Bar = "sausage";
foo.Boo = 4;

FuncCall( move( foo ) );
Run Code Online (Sandbox Code Playgroud)

Foo可能看起来像这样:

class Foo
{
public:
    Foo();

    string Bar;
    int Boo;
}
Run Code Online (Sandbox Code Playgroud)

为什么我不只是将所有内容放入构造参数中?

因为当你必须这么多时,这是愚蠢的:

Foo( int width, int height, string title, string className, string thjis, stihjrjoifger gfirejgoirejgioerjgoire ) 它一直在继续......虽然我已经拥有了我班级内的属性......所以只是想知道它是否可以完成..

c++ c++11 c++14

4
推荐指数
1
解决办法
369
查看次数

在c ++中,将一个动态分配的对象传递给一个函数(总是)一个坏主意?

我知道问题的标题看起来有点令人头疼,但我真的不知道怎么用一句话来问这个问题.我只会告诉你我的意思:

void f(T *obj)
{
    // bla bla
}
void main()
{
    f(new T());
}
Run Code Online (Sandbox Code Playgroud)

据我所知,(几乎)每个新的都需要一个删除,这需要一个指针(由new返回).在这种情况下,new返回的指针不存储在任何地方.这会是内存泄漏吗?

C++是否有某种神奇的功能(程序员看不见),它会在函数结束后删除对象,或者这种做法总是一个坏主意?

c++ pointers function

3
推荐指数
3
解决办法
1287
查看次数

通过引用将unique_ptr传递给函数

我注意到我正在编译的某个库中的某个行为,这有点出乎意料,并想澄清一下.

有一个类有以下形式的方法:

void notify(Frame & frame);
Run Code Online (Sandbox Code Playgroud)

现在,有一个调用者使用unique_ptr如下:

std::unique_ptr <Frame> localFrame (new Frame(rows, cols));
Run Code Online (Sandbox Code Playgroud)

现在当它调用方法时:

obj->notify(*localFrame);
Run Code Online (Sandbox Code Playgroud)

所以这依赖于底层指针到引用的隐式转换.

我的问题是,这是跨平台和预期的行为吗?我有什么用途可以做类似的事情:

obj->notify(*localFrame->get());
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers unique-ptr

1
推荐指数
1
解决办法
809
查看次数