- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想使用智能指针实现一个简单的引用计数。变量pointer
表示指向存储对象的指针,reference_count
表示对象的拷贝总数。
这是我的代码:
#ifndef smart_pointer_H
#define smart_pointer_H
template < typename T > class smart_pointer
{
private:
T* pointer;
int reference_count;
public:
smart_pointer() : pointer(0), reference_count(-1) {}
smart_pointer(T* p) : pointer(p)
{
if (p != NULL)
{
this->reference_count = 1;
}
else
{
this->reference_count = -1;
}
}
smart_pointer(const smart_pointer <T> & p) : pointer(p.pointer), reference_count(p.reference_count + 1) {}
bool operator == (const smart_pointer <T>& p) { return pointer == p.pointer; }
bool operator != (const smart_pointer <T>& p) { return pointer != p.pointer; }
~ smart_pointer()
{
if(-- reference_count == 0)
{
std::cout << "Destructing: " << '\n';
delete pointer;
}
}
T& operator * () { return *pointer; }
T* operator -> () { return pointer; }
smart_pointer <T> & operator = (const smart_pointer <T> & p)
{
if (this != &p)
{
if( -- reference_count == 0)
{
delete pointer;
}
pointer = p.pointer;
reference_count = p.reference_count + 1;
}
return *this;
}
};
这是我的测试代码,类示例存储 2D 点和两个指向任何其他 2D 点的指针。
template < typename T >
class smart_pointer;
class Point
{
private:
double x, y;
smart_pointer <Point> p1;
smart_pointer <Point> p2;
public:
Point(double xx, double yy): x(xx), y(yy) {this-> p1 = NULL; this->p2 = NULL;}
Point(double xx, double yy, smart_pointer <Point> p1, smart_pointer <Point> p2): x(xx), y(yy) {this-> p1 = p1, this->p2 = p2; }
double getX(){ return x;}
double getY(){ return y;}
void setX(double xx) {this->x = xx;}
void setY(double yy) {this->y = yy;}
void setP1(smart_pointer <Point> p1) {this->p1 = p1;}
void setP2(smart_pointer <Point> p2) {this->p2 = p2;}
void print()
{
std::cout << "x= " << x << " y= " << y << '\n';
std::cout << "p1" << '\n';
if (p1 != NULL)
{
p1->print();
}
std::cout << "p2" << '\n';
if (p2 != NULL)
{
p2->print();
}
std::cout << '\n';
}
};
二维点列表:
#include "Point.h"
class PointsList
{
private:
std::vector <smart_pointer <Point> > points;
public:
smart_pointer <Point> & operator [] ( int index ) {return points[index];}
public:
void push_back(smart_pointer <Point> p) {points.push_back(p);}
void erase(unsigned int index) {points.erase(points.begin() += index );}
void printPoints()
{
std::cout << "List of points" << '\n';
for (unsigned int i = 0; i < points.size(); i++)
{
points[i]->print();
}
}
};
测试代码:
#include "Point.h"
#include "PointsList.h"
int main()
{
smart_pointer <Point> pb = NULL;
pb = (new Point(0,0));
smart_pointer <Point> p0(new Point(0,0));
p0->print();
smart_pointer <Point> p1(new Point(10,10));
p1->print();
smart_pointer <Point> p2(new Point(20,20));
p2->print();
smart_pointer <Point> p3(new Point(30,30));
p3->print();
smart_pointer <Point> pa(p3);
p0->setP1(p2);
p0->setP2(p3);
p0->print();
p0 = p1;
p0->print();
p0->print();
PointsList pl1;
pl1.push_back(p0);
pl1.push_back(p1);
PointsList pl2;
pl2.push_back(p2);
pl2.push_back(p3);
pl1.erase(0);
pl1.printPoints();
pl2.printPoints();
return 0;
}
这种解决方案的优点或缺点在哪里?海量数据的运行速度、转换、继承可能出现的问题等。感谢您的帮助。
我对这个例子还有一个问题:哪种类型的智能指针(共享的,作用域的)最适合这样的数据结构:
//Class with cross-references to points p1, p2
class PointTopo
{
private:
double x, y;
PointTopo * p1;
Point * p2;
public:
PointTopo(double xx, double yy): x(xx), y(yy) {this-> p1 = NULL; this->p2 = NULL;}
...
};
//Class with cross references: topological model for Delaunay triangulation
class Edge
{
private:
Point2D * start;
Edge *next;
Edge *previous;
Edge *twin;
...
};
感谢您的帮助...
最佳答案
您的引用计数不起作用。
如果您将两个智能指针一起复制或赋值,它们需要使用相同的位置来执行计数。
目前每个对象都有自己的计数,因此它们可能会不同步。
smart_pointer<int> x(new x); // x.pointer: <good> x.reference_count: 1
{
smart_pointer<int> y; // y.pointer: NULL y.reference_count: -1
y = x; // x.pointer: <good> x.reference_count: 1
// y.pointer: <good> y.reference_count: 2
smart_pointer<int> z;
x = z; // x.pointer: NULL x.reference_count: 0 (BAD)
// z.pointer: NULL z.reference_count: -1
// y.pointer: <bad> (it was deleted by x) y.reference_count: 2
}
按照评论中的要求说明问题。
此时。我们刚刚创建 z 的地方。但还没有完成 x = z;
x { pointer: 0xabcd1234 reference_count: 1 }
y { pointer: 0xabcd1234 reference_count: 2 }
z { pointer: NULL reference_count: -1 }
// So here is your assignment operator.
// Doing the `x = z` we will walk through the following code.
//
smart_pointer <T> & operator = (const smart_pointer <T> & p)
{
if (this != &p)
{
// We get here.
// Left hand side is 'x' so this condition will be true.
if( -- reference_count == 0)
{
// Now we are deleting a pointer.
// That is held by 'x'
delete pointer;
// But 'y' is holding a pointer with the same value.
// Now y is holding a pointer to a deleted variable.
}
// Here we copy 'z' into 'x'
// Copy the pointer. That happens to be NULL.
pointer = p.pointer;
// Now we copy and increment the reference count.
// So 'x' has a value of 0 while 'z' has a value of -1.
// This also breaks the invariant on 'x' that NULL values should
// have a reference count of -1 (as X is NULL and ref-count is 0)
reference_count = p.reference_count + 1;
}
return *this;
}
如果有人现在尝试使用“y”,我们就会有未定义的行为,因为它包含指向已取消分配的内存的指针。
#include <vector>
template<typename T>
class SP
{
T* object;
size_t* count;
public:
SP(T* data)
try
// Use weird try around initializer list to catch new throwing.
// If it does we delete data to stop it from leaking.
:object(data)
,count(data ? new int(1) : NULL)
{ /* This is the constructor */}
catch(...)
{delete data;}
SP(): object(NULL), count(NULL) {}
//SP(T* data): object(data), count(new int(1)) {} // Lined up here so it look neat but implemented above to use weird try catch
SP(SP<T> const& rhs): object(rhs.object), count(rhs.count) {if (count) {++(*count);}}
SP<T>& operator=(SP<T> rhs) // Note implicit copy construction in rhs
{
// Using copy swap idiom for assignment.
// The copy is hidden because the parameter is pass by value.
this->swap(rhs);
return *this;
}
void swap(SP<T>& rhs) throw()
{
std::swap(object, rhs.object);
std::swap(count, rhs.count);
}
~SP()
{
if ((count) && (--(*count) == 0))
{
delete count;
delete object;
}
}
};
关于c++ - 简单引用计数 : smart pointers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613299/
我正在尝试向自定义操作添加一个按钮,但未在操作中添加一个新列,使该按钮与其他按钮重叠。 代码: settings = { actions: { custom: [
背景 一段时间以来,作为用户,我注意到在某些应用程序上我可以对部分文本执行常见操作,例如: 这称为“智能文本选择”(视频 here ),它从 Android O 开始就有了,如图 here . 问题
我有一个带有 W3EditBox 的 modalDialog,用户在其中输入一个字符串,该字符串最终被添加到主窗体上的 W3ListBox 如果存在这些条件之一,我试图阻止 modalDialog 关
经过一番研究,我得出结论,这个应用程序的 ID 为 org.volt.apps , 但通过 tizen.application.launch 启动它或 tizen.application.launch
我正在评估 SMS,并且非常享受这种体验。我需要使用 window.open() 在浏览器上打开一个新窗口/选项卡。我已经使用 asm block 完成了这项工作,但我忍不住觉得有一种我不知道的更好的
我在Third Web上部署了一份智能合同,但我没有加入其中。在资源管理器页面上,智能合同的功能在第三个网站的仪表板上看不到。我是否仍然可以使用相同的部署智能合同来构建我的DApp?或者我应该从头开始
我有一定数量的集合,每个集合都包含可变数量的唯一数字 - 在它们所属的集合中是唯一的,而在其他集合中找不到。 我想制作一个最好在 Python 中实现的算法 - 但它可以是任何其他语言 - 从这些集合
我正在寻找一种“聪明”的方法来连接带有分隔符的字符串。显然我可以自己编写所有这些代码,所以我想知道是否有一种简单的方法(LINQ 或我不知道的其他方法)来执行此操作。 假设我有一组字符串(这可以是任意
我正在从事一个非常关键的性能项目,其中每个时钟周期都在我最重要的内部循环中。我正在考虑重构代码以隐藏指令延迟,但我想知道现代 CPU 的乱序执行硬件在多大程度上已经为我做到了这一点。考虑以下(简单的、
我有gradle的spring boot项目。我想使用json-smart库来处理存储在MySQL String记录中的小型json数据。 这是我对build.gradle的依赖: dependenc
我想在飞利浦电视(NETTV 型号)浏览器中提供我自己的页面预览图像。显示在历史页面地址附近。 我该怎么做? (图片来自avforum,通过google搜索获得) 飞利浦电视使用 Opera 11
“计数”列接收的数据是一个 int (0, 1, 2, ...)。对于 0 的单元格不应显示任何内容,当数据 > 0 时,应显示图像。 count.setImageURLPrefix("silk/
我是否应该构建一个自动完成的“智能”,因为每个结果都必须隐藏在一系列信息之后。 例如: 我正在寻找“博洛尼亚”,我必须运行一个查询(或多个查询,我正在寻找一种尽可能少的方法),在“名称”字段中搜索“博
知道如何做到这一点吗?我想为我的类(class)制作类似 HDTUNE PRO 的东西,但我不知道如何在 java 中读取 HDD Smart,这可能吗?它专为 Windows 设计 最佳答案 您将需
我想进行“智能”正则表达式替换。例如,考虑: import re str = "x(x) and y(y)" res = re.sub("\(.*?\)","z", str) 结果是: xz and
所以我有一个 UI 元素(单行文本),我希望它相对于整个设备水平居中——除非/直到它与给定 View 组/布局中的其他 UI 元素发生冲突。那时我希望它要么在剩余空间中居中,要么在不发生碰撞的情况下尽
我希望我能很好地解释我的问题,以便有人提供帮助。 基本上,我有一个水平滚动条(范围为 0 到 1000)和一个表示滚动条位置除以 1000 的编辑控件,以便用户可以使用滚动条选择 0 到 1 之间的数
我正在尝试制作一个自动缩进的文本区域,到目前为止它可以使用以下代码。我遇到的问题是,我目前正在阻止默认操作,即按 enter 键计算行中的制表符,然后再插入换行符。 这行得通,但我想要文本区域的默认操
我正在为我的基于 php 的网站创建一个搜索引擎。我需要搜索一个 mysql 表。 问题是,搜索引擎必须非常“智能”,这样用户才能轻松找到他们的元素(这是一个分类网站)。 我目前已经使用这段代码设置了
如何智能解析页面搜索结果返回的数据? 例如,假设我想创建一个网络服务,通过解析许多图书提供商网站的搜索结果来搜索在线图书。我可以获得页面的原始 HTML 数据,并执行一些正则表达式以使数据适用于我的
我是一名优秀的程序员,十分优秀!