相关疑难解决方法(0)

为什么shared_ptr有一个显式的构造函数

我想知道为什么shared_ptr没有隐式构造函数.这里没有提到的事实:为此获取boost :: shared_ptr

(我想出了原因,但认为无论如何发布都是一个有趣的问题.)

#include <boost/shared_ptr.hpp>
#include <iostream>

using namespace boost;
using namespace std;

void fun(shared_ptr<int> ptr) {
    cout << *ptr << endl;
}

int main() {
    int foo = 5;
    fun(&foo);
    return 0;
}

/* shared_ptr_test.cpp: In function `int main()':
 * shared_ptr_test.cpp:13: conversion from `int*' to non-scalar type `
 *  boost::shared_ptr<int>' requested */
Run Code Online (Sandbox Code Playgroud)

c++ boost refcounting

4
推荐指数
2
解决办法
3043
查看次数

C++中对象构造时的唯一指针错误

我在使用 unique_ptr 创建对象时出错,如下所示错误:从 Account * 到非标量类型 std::unique_ptr 的错误转换

std::unique_ptr <Account> acc_ptr = new Account(100);
Run Code Online (Sandbox Code Playgroud)

如果我使用如下原始指针,则没有错误

Account *acc_ptr = new Account(100);
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

c++ unique-ptr

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

标签 统计

c++ ×2

boost ×1

refcounting ×1

unique-ptr ×1