阿列克谢的单身人士 [英] Alexei's singletons

查看:87
本文介绍了阿列克谢的单身人士的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题关于单身人士的Alexei Alexandrescu的章节'

C ++现代设计。


1.在本章的开头,阿列克谢说单身一词。 class

由静态成员函数实现的问题是

函数不是虚函数,所以你必须触及
$中的类''代码b $ b为了改变行为。但是,单身人士如何被继承

来自?这个独特实例的具体类是不是已经硬编码了?
单例的实现?


2.我们提出他的SetLongevity实用程序,他使用realloc而不是

new / delete,认为如果不这样做,那么

SetLongevity维护的数据结构将遭受与其他相同的长寿问题

单身人士。我不明白,因为它们在

atexit()上被彻底删除了。有人可以向我解释一下吗?


谢谢,

铁托

I have two questions about the singletons'' chapter of Alexei Alexandrescu''s
"C++ Modern Design".

1. In the beginning of the chapter Alexei states that a "singleton" class
implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class'' code in
order to change the behaviour. But, how is a singleton meant to be inherited
from? Is not the concrete class of the unique instance already hardcoded in
the singleton''s implementation?

2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?

Thanks,
Tito

推荐答案

*铁托 < TI *************************** @ inicia.es> schriebt:
* "Tito" <ti***************************@inicia.es> schriebt:

我有两个关于Alexei Alexandrescu的C ++现代设计的单身人士章节的问题。


那一定是安德烈的鲜为人知的兄弟。


1.在本章的开头,阿列克谢说单身人士 ;类静态成员函数的实现有一个问题,即
函数不是虚函数,所以你必须触摸类中的代码才能改变行为。但是,单身人士是如何被继承的呢?


与任何其他课程一样。


不是单独的实例中已经硬编码的独特实例的具体类实施?


如果您使用静态成员函数模式,那就是点数。

2。我们提出他的SetLongevity实用程序,他使用realloc代替
new / delete,认为如果不这样做,SetLongevity维护的数据结构将遭受与其他相同的长寿问题
单身。我不明白,因为它们在
atexit()上被彻底删除了。有人可以向我解释一下吗?

I have two questions about the singletons'' chapter of Alexei Alexandrescu''s
"C++ Modern Design".
That must be Andrei''s little known brother.

1. In the beginning of the chapter Alexei states that a "singleton" class
implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class'' code in
order to change the behaviour. But, how is a singleton meant to be inherited
from?
As with any other class.

Is not the concrete class of the unique instance already hardcoded in
the singleton''s implementation?
It would be if you used the static member function pattern, and that''s
the point.
2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?




我不确定你在这里指的是什么。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么顶级发布这么糟糕的事情?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



I''m not sure what you''re referring to here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


"铁托" < TI *************************** @ inicia.es>在消息新闻中写道:< 2h ************ @ uni-berlin.de> ...

[snip]
"Tito" <ti***************************@inicia.es> wrote in message news:<2h************@uni-berlin.de>...
[snip]
2我们提出他的SetLongevity实用程序,他使用realloc代替
new / delete,认为如果不这样做,那么SetLongevity维护的数据结构将遭受与其他
单身人士。我不明白,因为它们在
atexit()上被彻底删除了。有人可以解释一下吗?
2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?




pTrackerArray中包含的对象在AtExitFn()中删除,

但是数组本身不是。由于我们不知道在调用AtExitFn()时pTrackerArray中包含了多少个对象,因此我们不知道删除数组时是否需要
。但是,如第143页的脚注

中所述,realloc()可以表现为malloc和free。如果您使用

size> 0来调用它,则表现为malloc,如果您使用size = 0调用它,则表示

为免费。这意味着我们可以使用数组中的

对象的数量来调用realloc(),当数组中的对象数量达到零时,数组就会自动生成删除。


T.



The objects contained in the pTrackerArray are deleted in AtExitFn(),
but the array itself is not. Since we do not know how many objects are
contained in pTrackerArray when AtExitFn() is called, we don''t know
when to delete the array. But realloc(), as explained in the footnote
on page 143, can behave as both malloc and free. If you call it with
size>0 it behaves as malloc, and if you call it with size=0 it behaves
as free. This means that we can just call realloc() with the number of
objects in the array, and when the number of objects in the array
reaches zero the array is automagically deleted.

T.


> 2.我们提出他的SetLongevity实用程序,他使用realloc而不是
> 2. We he presents his SetLongevity utility, he uses realloc instead of
new / delete,认为如果不这样做,那么SetLongevity维护的数据结构将遭受相同的长寿问题。作为其他单身人士。我不明白,因为它们在
atexit()上被彻底删除了。有人可以向我解释一下吗?
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?




快速更正:安德烈,不是阿列克谢。


请仔细阅读以跟踪器类型只有一个

的实例......


据我所知,TrackerArray本身就是一个单身人士。

但它必须由管理单例的函数使用。这意味着

你不能应用我们正在设计的单一机制

TrackerArray。

安德烈解决这个问题的方式是使用连续的c风格数组,而不是容器,并使用低级例程管理整个数组的

分配/释放
$ b保证随时可以工作的$ b。

AtExitFn确实在LifetimeTracker对象上使用delete(),因为SetLongevity

用new创建它们。但我们正在谈论一系列指向

LifetimeTracker对象的指针,其大小由重新分配控制。


HTH,

安迪。



Quick correction: Andrei, not Alexei.

Read carefully paragraph starting with "There is only one instance of
the Tracker type..."

It says, as I understand it, that TrackerArray is itself a singleton.
Yet it must be used by a function that manages singletons. That means
that you cannot apply singleton mechanism we''re devising to the
TrackerArray.
The way Andrei went about solving this problem was to
use a contiguous c-style array, not a container, and manage the
allocation/deallocation of the whole array with low-level routines
that are guaranteed to work at any time.
AtExitFn does use delete() on LifetimeTracker object, as SetLongevity
creates them with new. But we''re talking about an array of pointers to
LifetimeTracker objects whose size is controlled by reallocate.

HTH,
Andy.


这篇关于阿列克谢的单身人士的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆