为了避免不必要的变量,有没有办法使
int var = sharedPtrStringVar.length();
String 和其他函数可以与智能指针一起使用吗?
到目前为止,我一直将字符串从指针复制到临时变量中,但没有更好的方法(不使用向量)吗?
谢谢你 Yksisarvinen 这正是我想要知道的
我已经认识到,当移动取消引用Box的时*Box::new(_),它不会调用Deref::deref或DerefMut::deref_mut;它确实移动了值,这意味着*Box::new(_)拥有所有权,而不是对引用的取消引用。
一个例子:
let a = Box::new(String::from("hello");
let b = *a;
Run Code Online (Sandbox Code Playgroud)
我了解到这Box是一个非凡的结构,因此在数据移动的情况下,它实际上像引用一样取消引用(没有Deref特征)。
Box移动过程中,堆中分配的内存发生了什么变化?它被释放了吗?是用一堆零代替的吗?是否仍然没有任何访问方式?
我知道分配的内存在删除String::from时将被释放b。我对数据并不好奇str type hello,我对大小为 的内存感到好奇size of String。
我如何显式取消引用Box没有Deref特征的?当我尝试时,它会Box通过调用自动借用Deref::deref。
let a = Box::new(String::from("hello"));
fn test(i: &String) {}
test(&(*a));
Run Code Online (Sandbox Code Playgroud)
编译器推断不需要 move *a,因此看起来它是通过特征取消引用,而不是直接取消引用。
本例成功消耗了盒子和字符串:
let a = Box::new(String::from("hello"));
fn test(i: &String) {}
test(&(*a));
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <memory>
int main() {
int i = 0;
float f = 0.0f;
double d1 = 0.0, d2 = 0.0, d3 = 0.0, d4 = 0.0;
auto a = [i,f,d1,d2,d3,d4](){};
std::cout << sizeof(std::unique_ptr<decltype(a)>) << std::endl; // 8
std::cout << sizeof(std::unique_ptr<char, decltype(a)>) << std::endl; // 48
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我只添加一个字符时,为什么这个程序的输出是 48?
我正在阅读C++ 是否可以确定指针是否指向有效对象?该线程中的正确答案是不,你不能这样做,但该线程现在已经很旧了,我想知道是否有任何更改。我读到用智能指针这是可能的。那么如何才能实现这一目标呢?
我有一堂课
class Base {
public:
virtual ~Base() {}
virtual string returnString() = 0;
};
class B : public A {
public:
string returnString() { return "string"; }
}
class C : public A {
public:
string returnString() { return ""; }
}
Run Code Online (Sandbox Code Playgroud)
和功能
string returnStringFunction(...);
Run Code Online (Sandbox Code Playgroud)
我希望能够传递 C 类型的对象,但默认情况下,我希望此函数使用动态创建的 B 类型对象。
我记得使用过这样的东西:
string returnStringFunction(const A& a = *std::make_unique<B>())
Run Code Online (Sandbox Code Playgroud)
或者
string returnStringFunction(const std::unique_ptr<A> a = std::make_unique<B>())
Run Code Online (Sandbox Code Playgroud)
函数体示例:
string returnStringFunction(...) {
return a->returnString();
}
Run Code Online (Sandbox Code Playgroud)
但是这两个解决方案即使它们通常在我的“沙盒”环境中编译和工作,它们也会在工作区上生成SIGFAULT。知道是什么原因造成的或如何更好地解决它吗?
提前致谢
我正在调用一个库函数,它返回一个指向类对象的原始指针,但我想将返回的指针包装在unique_ptr. 我不知道库如何创建指针,但我假设它使用new(请参阅下面代码中的注释)。
下面的代码是创建unique_ptr并通过函数调用引用返回它的有效方法吗?该代码确实打印“42”,但是有更好的方法吗?
该库是 MariaDB C++ 连接器。不幸的是,它的文档很少。几天前我在SO上询问过这个问题,但到目前为止还没有得到答复。我本来会使用 MySQL 版本,但似乎并没有好多少。但是,有一个示例程序,其中包含以下行main:
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
Run Code Online (Sandbox Code Playgroud)
因此,在阅读了评论和答案后,我想我现在可以假设Connection我想要的对象已被new编辑。另一方面,为什么我通过引用传递而不是仅仅返回unique_ptr- 好问题。唯一的原因是该应用程序中的所有其他代码都返回 abool作为成功状态,因此我认为在这里执行相同的操作会更加一致。
原始代码:
#include <memory>
#include <iostream>
class A {
public:
int b;
A() : b(42) {}
};
void create(std::unique_ptr<A> &p) {
A *a = new A; // this is actually a function call which returns an A*
p.reset(a);
}
int main() {
std::unique_ptr<A> foo;
create(foo); …Run Code Online (Sandbox Code Playgroud) 我想定义两个类A,并I以它们的对象尊重这种关系的方式:
i1 -----------a1
|------a2
|----a3
Run Code Online (Sandbox Code Playgroud)
该类的一个实例I指向该类的零个、一个或多个实例A。
类的一个实例A仅指向该类的一个实例I。
类的实例I可以没有任何类的实例A,但类的实例必须“附加”A类的实例。I
为了满足这些条件,我将这两个类声明如下:
class I;
class A
{
private:
std::string as;
std::shared_ptr<I> iA;
public:
A(std::string aTxt);
A();
~A();
};
class I
{
private:
std::string ip;
std::vector<std::unique_ptr<A>> as;
friend class A;
public:
I(std::string i);
~I();
};
Run Code Online (Sandbox Code Playgroud)
在源文件中,我以这种方式定义了这两个类:
A::A(std::string aText)
{
as = aText;
}
A::A()
{
as = "";
}
A::~A()
{
}
I::I(std::string i)
{ …Run Code Online (Sandbox Code Playgroud) 所以我有这个代码:
#include <iostream>
#include <list>
#include <string>
#include <memory>
using namespace std;
int main() {
{
shared_ptr<string> str = new string("Marius");
cout << str + " MMG";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过编译它:
clang++ -Wall -g -std=c++14 test.c++ -o test
Run Code Online (Sandbox Code Playgroud)
我明白了:
test.c++:11:22: error: no viable conversion from 'string *' (aka 'basic_string<char> *') to 'shared_ptr<string>'
shared_ptr<string> str = new string("Marius");
Run Code Online (Sandbox Code Playgroud)
哪里出错了?使用GCC我得到了同样的错误.
真正简单的问题:
我对C ++中的智能指针有点陌生。我想我拥有所有权的内容,但是我不知道如何访问他们实际指向的内容。当我尝试使用对象的成员函数/变量时,我只是获得unique_ptr类的函数,这不是我想要的。
我有一个执行类的下面程序,该程序填充下面所示的映射
map<string,map<string,vector<StructAbsTypeObject>>>
Run Code Online (Sandbox Code Playgroud)
在这里,我正在创建共享对象并分配它们,它们在第一次检查时有效,但是在第二次检查时,shared_ptr返回null。我需要知道原因。该代码看起来不错,但不知道哪里出了问题。
//Code begins
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <memory>
using namespace std;
class Test {
public:
Test(int i):t(i) {
}
private:
int t;
};
class ConcTypeObject {
public:
ConcTypeObject() {
}
ConcTypeObject(const ConcTypeObject& other) {
m_ptr_Test = other.m_ptr_Test;
}
ConcTypeObject& operator=(const ConcTypeObject& other) {
m_ptr_Test = other.m_ptr_Test;
}
void setTest(shared_ptr<Test> ptr) {
cout << "setTest" << endl;
m_ptr_Test = ptr;
}
shared_ptr<Test> getTest() {
return m_ptr_Test;
}
bool isValid() {
if(m_ptr_Test) {
return true; …Run Code Online (Sandbox Code Playgroud) smart-pointers ×10
c++ ×9
shared-ptr ×3
box ×1
c++11 ×1
dereference ×1
memory-leaks ×1
pointers ×1
rust ×1
std ×1
stdvector ×1
unique-ptr ×1