- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的应用程序使用了大量的 Panda
对象。每个 Panda
都有一个 Bamboo
对象列表。一旦 Panda
被初始化(没有添加或删除 Bamboo
对象),这个列表就不会改变。目前,我的类实现如下:
class Panda
{
int a;
int b;
int _bambooCount;
Bamboo* _bamboo;
Panda (int count, Bamboo* bamboo)
{
_bambooCount = count;
_bamboo = new Bamboo[count];
// ... copy bamboo into the array ...
}
}
为了减轻分配 Bamboo
对象数组的开销,我可以按如下方式实现这个类——基本上,不是通过常规构造函数创建对象,构造方法分配一个内存块同时保存 Panda
对象及其 Bamboo
数组:
class Panda
{
int a;
int b;
Panda ()
{
// ... other initializations here ...
}
static Panda *createPanda (int count, Bamboo* bamboo)
{
byte* p = new byte[sizeof(Panda) +
sizeof(Bamboo) * count];
new (p) Panda ();
Bamboo* bamboo = (Bamboo*)
p + sizeof(Panda);
// ... copy bamboo objects into the memory
// behind the object...
return (Panda*)p;
}
}
除了增加维护工作量之外,您能预见第二种设计的任何问题吗?这是一个可以接受的设计模式,还是只是一个过早的优化,以后可能会回来咬我?
最佳答案
C++ 为您提供了另一种选择。您应该考虑使用 std::vector。
class Panda
{
int a;
int b;
std::vector<Bamboo> bamboo;
// if you do not want to store by value:
//std::vector< shared_ptr<Bamboo> > bamboo;
Panda (int count, Bamboo* bamb) : bamboo( bamb, bamb+count ) {}
}
如果您想将 Pandas 和竹子存储在连续内存中,您可以使用 this article 中的解决方案.主要思想是重载operator new
和operator delete
。
关于c++ - 变长对象 : Ever a good idea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1390073/
我正在使用 pytesseract(tesseract 版本 3.05)对以数字方式创建的打印 PDF 帐单进行 OCR(光学字符识别)。我对其进行预处理以去除任何颜色并将其设置为纯黑白和 600 D
以下是我尝试运行的代码,输出是Good。那么,我们可以使用类实现的接口(interface)的变量吗? interface IDummyInterface { public String TY
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在编写一个 Java 客户端,它通过 HTTP/XML 与远程服务器通信。 服务器以 XML 格式向我的客户端发送命令,如下所示: C1 ..... 大约有 10 个或更多不同的命令(C1
我在 android 应用程序上有一个奇怪的问题,我没有这样的表异常,但我确定数据库存在。对于某些将我重定向到此处其他帖子的人,我想补充一点,我正在自己的手机上尝试该应用程序而不是模拟器,这是因为我在
我在谷歌上搜索 RNGCryptoServiceProvider,其中包含有关如何限制最大值和最小值之间的范围并仍然获得均匀分布的示例。在我使用模运算符之前,但有时我会得到奇怪的值(高于最大值)...
这个问题在这里已经有了答案: calling constructor of a class member in constructor (5 个答案) 关闭 4 年前。 我有一个非指针类成员需要在构
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭10
作为hadoop配置什么好.. 大量小型机器,每台具有 512 MB Ram 或少量大型机器(大约 2Gb 或 4GB Ram) 我可以选择两者中的任何一个,因为我的节点是虚拟机.. 请分享您的想法.
伙计们,我是vue新手所以不知道如何实现以下情况我如何获取当前选定行的数据这是代码
我经常访问一个名为 GOOD 的网站我特别喜欢一种审美风格;导航栏如何在网站背景中扩展其颜色。如果您访问该网站,就会明白我的意思。 在 CSS 中,我怎样才能以最简单的方式复制它?我已经用 z-ind
我有一个存在主义假设,例如: H : exists (a : A) (b : B) (c : C), P a b c 我想分解为: a : A b : B c : C H0 : P a b c 战术d
在 Github 上,我注意到一些拉取请求说它们“适合合并”,因为它们通过了 Travis构建通过。我已经使用了一点 Travis,它如何与 Github 上的拉取请求集成? 这是我正在谈论的内容的屏
刚刚在 Apple docs: 中找到 Note: Although good for occasional communication between threads, you should not
我正处于一个应用程序的概念阶段,该应用程序将有大量音频/视频输入和输出。我想用Java来做;但不知何故我还没有完全相信。你怎么认为?到底能有多糟糕?有什么建议吗? 为什么我想到 Java: 这是我最熟
我正在用 Java 重新实现 .Net API,该 API 指定了一大堆事件,但 java 并不隐式支持。 我将使用观察者模式,但由于事件的数量,我真的不想让界面变得困惑。 我想知道声明一个“Even
我的类有很多方法,其中一些方法是用注释标记的。每个方法可以有很多注释,例如 @StepAnnotation(name="Action1" ) @SequenceAnnotation(name="tra
启用GD后(通过调用enableSecureCommunication或对应用程序进行身份验证),将自动保护NSURLConnection。它是如何做到的? 最佳答案 我的猜测是,Good Dynam
我是 Vue 的新手并且陷入了困境,如果有人建议我如何做到这一点,我不知道该怎么做,让我先展示我的代码 save 并在脚本中 data(){ return{
考虑以下代码: private List types; if(!getTest().contains(type)) { return Color.LIGHT_GRAY;
我是一名优秀的程序员,十分优秀!