鉴于下面的代码,unique_ptr和Bar之间的区别是什么,bar ( { new int } );
但没有foo( { new int } );
#include <memory>
struct Bar {
Bar() = default;
Bar( int* m ) : m_( m ) {};
~Bar() { if ( m_ ) delete m_; }
explicit operator bool() const { return m_; }
private:
int* m_ {};
};
bool bar( Bar && a ) { return bool(a); }
bool foo( std::unique_ptr<int> && a) { return bool(a); }
int main() {
bar( { } ); …
Run Code Online (Sandbox Code Playgroud) 有一些宏可以防止类被复制,例如: 宏禁止类复制和赋值.谷歌-vs- Qt
仅仅通过在我班级中使用unique_ptr,我会获得相同的结果吗?如果是这样,有没有理由不这样做?例如
class Foo {
private:
std::unique_ptr<int> DISABLE_COPY;
};
Run Code Online (Sandbox Code Playgroud) 我按照V. Romeo的实体管理教程(在GitHub和Youtube上).
然后我尝试重写类CEntity,CComponent和测试CPosition(主要来自Romeo视频/代码的内存).我遇到的问题是,在我的主要我在堆栈上创建一个CEntity并添加一个组件.当我通过i添加组件时,addComponent()
获取对新创建的组件的引用,返回addComponent()
.
当我现在想要通过返回的引用修改组件时,我所做的更改不会反映回实体(组件).看起来像是对我的悬空参考,但我无法找到我犯的错误.
谁能指出我在这里做错了什么?
掌握我的CEntity课程:
#include <array>
#include <bitset>
#include <memory>
#include <cassert>
#include <stdexcept>
namespace inc
{
using ComponentID = unsigned int;
ComponentID getNewID()
{
static ComponentID id = 0;
return id++;
}
template <typename T>
ComponentID getComponentID()
{
static ComponentID component_id = getNewID();
return component_id;
}
// Forward declarations used by CEntity:
struct CComponent;
class CEntity
{
public: …
Run Code Online (Sandbox Code Playgroud) 我正在使用VS2012.
我将代码从原始指针移植到unique_ptr并面临问题.
在这里,我试图模拟场景:
class xyz{
public:
virtual int getm();
int get();
static std::unique_ptr<xyz> returnbase();
};
class abc:public xyz
{
public:
int getm() {return 0;}
};
std::unique_ptr<xyz> xyz::returnbase()
{
std::unique_ptr<xyz> u_Swift(nullptr);
u_Swift = std::unique_ptr<xyz>(dynamic_cast<xyz*>(new abc()));
return u_Swift;
}
int _tmain(int argc, _TCHAR* argv[])
{
xyz* x1 = xyz::returnbase().get();
x1->get();
x1->getm();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
调用虚函数时出现"访问冲突"崩溃.
我很惊讶为什么这会导致虚拟功能崩溃?
通过观察我可以看到分配后虚拟指针已损坏.但为什么这个被腐蚀了,我很好奇.
参考以下代码
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::make_unique;
struct Base {};
struct Derived : public Base {};
int main() {
auto base_uptr = std::unique_ptr<Base>{make_unique<Derived>()};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
哪个构造函数被调用unique_ptr
?我看了一下cppreference.com和我发现的构造函数(前c ++ 17)(为了完成)
constexpr unique_ptr();
constexpr unique_ptr( nullptr_t );
explicit unique_ptr( pointer p );
unique_ptr( pointer p, /* see below */ d1 );
unique_ptr( pointer p, /* see below */ d2 );
unique_ptr( unique_ptr&& u );
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个可调用的对象,它基本上有一个绑定的unique_ptr
内部.我已经阅读了如何将unique_ptr捕获到lambda表达式中?
但我无法进一步传递lambda.以下代码无法编译,表明unique_ptr
正在进行复制.我不太明白为什么要尝试复制unique_ptr
.
#include <iostream>
#include <memory>
#include <functional>
using namespace std;
void f(std::function<void(int)> unary) {
unary(42);
unary(1337);
}
struct A{
void woof(int i) {cout<< "woof " << i << "\n";}
};
int main(){
auto another_unique = make_unique<A>();
auto bound_p = [ p = move(another_unique) ] (int i) {p->woof(i);};
bound_p(5);
f( bound_p ); // does not compute
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在调用中定义lambda内联f
,这样它就可以成为一个临时对象了,它可以被移入f
.但错误是一样的.
是否可以将这样的对象绑定/包装成callables并传递它们?
我的用例取决于unique_ptr
,但我怀疑任何删除副本c'tor的对象都会出现同样的问题.如果我错了,请告诉我或编辑主题不太通用.
我使用以下方式分配了三维:
board = new char**[depth];
for (int d = 0; d < depth; d++)
{
board[d] = new char*[rows];
for (int i = 0; i < rows; i++) {
board[d][i] = new char[cols];
for (int j = 0; j < cols; j++) {
board[d][i][j] = 'k';
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用unique_ptr来管理它,但我不知道如何使用unique_ptr公开的接口启动这样的结构.
我正在构建一个Node类,其中左侧和右侧子节点具有unique_ptr,父节点具有Node*指针.当我删除节点时,我接受节点,我必须检查我正在删除的节点是否是左子节点,然后重置父节点中的unique_ptr.有没有办法获取指针,并询问是否有任何unique_ptr包装,并可能返回它?
#include <iostream>
#include <memory>
class Base
{
public:
virtual void foo() = 0;
};
class Derived : public Base
{
public:
void foo() override { std::cout << "Derived" << std::endl; }
};
class Concrete
{
public:
void Bar() { std::cout << "concrete" << std::endl; }
};
int main()
{
std::unique_ptr<Concrete> ConcretePtr = nullptr;
ConcretePtr->Bar();
std::unique_ptr<Base> BasePtr;
BasePtr->foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我假设将一个unique_ptr声明为具体类型Concrete
,为类型的对象分配内存,Concrete
而unique_ptr开始指向它.我的假设/理解是否正确?我问,因为ConcretePtr->Bar();
打印机"混凝土"到控制台.但是,如果我创建一个指向接口的唯一指针Base
,它不知道我需要的确切对象类型,也不知道在内存中分配/获取资源.
这失败BasePtr->foo();
了BasePtr._Mypair._Myval2 was nullptr.
为什么第一个声明std::unique_ptr<Concrete> ConcretePtr = nullptr; …
Java很久,第一次使用C++.
我正在编写一个包装现有C++ API的Java类,看起来像这样:
public class Foo implements Closeable {
private long handle;
public Foo(File file) {
this.handle = Foo.openFile(file.toString());
}
// other methods go here...
public void close() {
Foo.closeFile(this.handle);
this.handle = 0;
}
private static native long openFile(String file);
private static native void closeFile(long handle);
}
Run Code Online (Sandbox Code Playgroud)
我们将指向本机C++对象的指针填充到handle
字段中.
问题是C++库没有给我一个指针,它给了我一个unique_ptr<Foo>
.所以这必须被移到堆上openFile()
(因此它不会超出范围),然后被销毁closeFile()
.
我无法让这个工作,我不确定我是否正确地做到了.在C++中正确执行此操作的最简单/最简洁的方法是什么?
这是我目前正在做的事情:
// This holds a unique_ptr<T>, typically on the heap, until no longer needed
template <class T>
class PointerHolder {
public:
PointerHolder(unique_ptr<T> …
Run Code Online (Sandbox Code Playgroud)