我想知道为什么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) 我在使用 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)
为什么会这样?