我将智能指针的实例存储到容器中时遇到问题.这是指针的代码.
#include "std_lib_facilities.h"
template <class T>
class counted_ptr{
private:
T* pointer;
int* count;
public:
counted_ptr(T* p = 0, int* c = new int(1)) : pointer(p), count(c) {} // default constructor
explicit counted_ptr(const counted_ptr& p) : pointer(p.pointer), count(p.count) { ++*count; } // copy constructor
~counted_ptr()
{
--*count;
if(!*count) {
delete pointer;
delete count;
}
}
counted_ptr& operator=(const counted_ptr& p) // copy assignment
{
pointer = p.pointer;
count = p.count;
++*count;
return *this;
}
T* operator->() const{ return pointer; }
T& operator*() …Run Code Online (Sandbox Code Playgroud) 保持成员变量指针引用COM对象并通过C++中的类重新引用引用是否有任何问题.
有没有人知道你每次想要使用COM对象时都想要调用.CreateInstance的原因,即你每次都得到一个新的实例.
我看不出有什么理由想要这样做,
谢谢,
(不是可以接受的答案!)
我想使用std :: make_shared来创建一个void指针.因为make_shared应该比shared_ptr(new T)快,并且异常保存我想知道是否有一个库函数以make_shared方式创建shared_ptr(new foo).
我有一些方法没有在互联网上的任何cpp引用中引用
例如在"内存"中,Shared_ptr有一个名为"_Expired"的方法
如果ptr过期,它返回一个布尔值
我以为只有weakptr才有这个......
我知道auto_ptr搞砸了复制语义,因此在容器中使用是不安全的,因为将一个auto_ptr复制到另一个会使source = NULL指针(这不是像移动语义一样吗?).但话说再说一次,unique_ptr根本无法复制,只能转让所有权.那么,unique_ptr如何在需要使用复制操作来复制和重新排列元素的容器和算法中使用?
在下面的情况下伪造共享指针的最佳方法是什么,你知道它没问题?
#include <memory>
struct Target {
bool ok() { return true; }
};
struct Monitor {
// Take a shared pointer as we will be using it later
Monitor(std::shared_ptr<Target> target)
: target(target)
{ }
bool check() {
// Use the shared pointer we grabbed before
return this->target->ok();
}
std::shared_ptr<Target> target;
};
// This function does not take a shared pointer because it does not
// hold on to the object after returning.
bool checkTargetOnce(Target& t)
{
// We have to …Run Code Online (Sandbox Code Playgroud) 从"一个不应该使用原始指针"成语的角度出发,我正在寻找智能指针来找到哪一个最适合引用关系.
通过引用关系,我的意思与此代码相同:
class A
{
public:
A(B & b) :
m_refB( b )
{ }
private:
B & m_refB; // A refers to B
};
Run Code Online (Sandbox Code Playgroud)
但是没有使用引用的所有缺点(以后不能绑定,不能重新分配,A不能再默认构造等).
然而,每个智能指针以其自己的语义表达所有权概念.它们甚至以这种所有权概念命名(当所有权仅对一个对象是唯一的时,唯一指针,当所有权在更多对象之间共享时共享指针等).
我想表达的是A指的是B,但是A不拥有B.可以std::reference_wrapper< B >做这个工作,或者那是错误的用法吗?
有没有办法为类编写一个拷贝构造函数(比如说Copyable,它可以保存std::unique_ptr一个Base类(但实际上是存储Derived对象).
快速测试显示预期的切片发生,因为Copyable不知道它所持有的实际类型.所以我想一个clone方法是必要的,但我想知道是否有办法让编译器以更好的方式处理它?
切片代码:
#include <algorithm>
#include <iostream>
#include <memory>
struct Base
{
Base(int i = 0) : i(i) {}
virtual ~Base() = default;
int i;
virtual int f() { return i; }
};
struct Derived : Base
{
Derived() = default;
virtual int f() override { return 42; }
};
struct Copyable
{
Copyable(std::unique_ptr<Base>&& base) : data(std::move(base)) {}
Copyable(const Copyable& other)
{
data = std::make_unique<Base>(*other.data);
}
std::unique_ptr<Base> data; …Run Code Online (Sandbox Code Playgroud) smart-pointers deep-copy copy-constructor move-semantics c++11
我已经阅读了很多问题,但没有人回答我的具体情况.
其实我有
std::vector<Point2Dd> points;
std::vector<Triangle> triangles;
Run Code Online (Sandbox Code Playgroud)
Point2Dd 是一个2D点的类,指定它的实现方式并不重要.
然而三角实现如下:
class Triangle{
public:
Triangle();
Triangle(Point2Dd* p1, Point2Dd* p2, Point2Dd* p3);
// Getter & setter
private:
Point2Dd* vA = nullptr;
Point2Dd* vB = nullptr;
Point2Dd* vC = nullptr;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,作为点向量的三点.
实际上它工作得很好,但我想:如果我在向量中添加另一个点,我的向量会更改所有内存地址吗?我的所有三角形都将由无效地址组成.
我读过关于使用的内容,std::unique_ptr<Point2Dd>但我认为这不是最好的方法.
你有什么解决方案吗?谢谢 :)
---编辑1 ---
为了澄清我的问题,我解释了我想要解决的问题.我正在做一个增量的Delaunay三角测量(没问题).所以我必须每次添加一次并更新我的三角测量.
所以我认为将三角形作为指向我的点的三个指针.我还有一个dag(节点 - >三个孩子的三角形)和一个保存相邻三角形的结构.
这就是为什么我认为总是使用指针,所以我不必在三个不同的结构中复制相同的点.
这就是我需要解决此问题以防止内存重新分配的原因.
我是Rust的新手,并且写作以了解Rust中的“智能指针”。我对智能指针在C ++中的工作方式有基本的了解,并且自几年前以来就一直将其用于内存管理。但是令我非常惊讶的是,Rust也明确提供了这样的实用程序。
因为在这里的教程(https://pcwalton.github.io/2013/03/18/an-overview-of-memory-management-in-rust.html)中,似乎每个原始指针都自动包装了一个智能指针,这似乎非常合理。那么,为什么我们仍然需要这样Box<T>,Rc<T>和Ref<T>东西?根据此规范:https : //doc.rust-lang.org/book/ch15-00-smart-pointers.html
任何评论将被感动很多。谢谢。