哪里放一个常数? [英] Where to place a constant?
问题描述
我有自己的字符串类,与
std :: string相比,我通常都非常喜欢。出于同样的原因,我正在努力改进它。
因为我的项目中的manny对象将包含空字符串"我认为在b / b
所有未初始化的字符串之间共享空字符串分配是个好主意。
但是我有不知道在哪里放置这个常数? :-S
我试过这个:
class CBL_String
{
public:
static char * strEmptyString ="" ;; //它抱怨这个...
};
如果我不在课堂规范中初始化它但是在cpp文件中我不确定
它是否在使用之前实际初始化,由其他全局CBL_String
变量...对吧?
char * CBL_String :: strEmptyString ="" ;;
建议有人吗?
-
Lasse
I have my own string class that I''m generally verry fond of compared to
std::string. For that same reason I''m trying to improve a little on it.
Since manny objects in my project will contain strings of empty size "" I
thought it would be a good idea to share en empty string allocation between
all uninitialized strings.
However I have no idea where to place this constant? :-S
I tried this:
class CBL_String
{
public:
static char * strEmptyString = ""; // it complaines about this...
};
If I do don''t initialize it in the class-spec but in a cpp file I''m not sure
that it is actually initialized before used, by some other global CBL_String
variable... right?
char *CBL_String::strEmptyString = "";
Suggestions anyone?
--
Lasse
推荐答案
2004年2月16日星期一12:23:11 +0100,Lasse Skyum < no spam>写道:
On Mon, 16 Feb 2004 12:23:11 +0100, "Lasse Skyum" <no spam> wrote:
建议任何人?
使用std :: string你会好得多。
You''d be much better off using std::string.
建议任何人?
你''使用std :: string会好得多。
You''d be much better off using std::string.
好吧......也许是这样,但我真的不知道为什么? CBL_String以不同的方式帮助我进行manny
从来没有成为我程序中任何错误的来源。
std :: string是否有共享的空字符串或者它使用NULL作为
空字符串?
如果std :: string如果没有初始化也分配了一个空字符串,那么我看到
没有理由使用它...
顺便说一句,我也写了我自己的CBL_ArrayList作为向量的等价物...
ony difference是设置标志时自动收缩的能力。对于
一些奇怪的原因它在我的插入/删除基准测试中也超过了矢量,
可能只是一堆设置或其他东西。
我确定所有这些也都在Boost中(或者可能是什么样的C ++扩展)但是我已经学到了很多东西都是我自己写的。
无论如何......我只是想要一个初始化的常量:-)可能还有
与其他东西相关!
- -
Lasse
Okay... maybe so, but I don''t really se why? CBL_String helps me in manny
different ways and has never been the source of any bugs in my programs.
Does std::string have a shared empty string or maybe it uses NULL as an
empty string?
If std::string also allocated an empty string if not initialized, then I see
no reason what so ever to use it...
BTW, I''ve also written my own CBL_ArrayList as an equivalent to vector...
ony differenceis the ability to automatically shrink if a flag is set. For
some odd reason it also outperformes vector in my insert/remove benchmarks,
probably just a bunch of settings or something.
I''m sure all this is also in Boost (or whatever C++ extentions there might
be) but I''ve learned so much be writing it my self.
Anyway... I just wanted to have an initialized constant :-) Might also be
relevant for other things!
--
Lasse
Lasse Skyum< no spam>写道:
Lasse Skyum <no spam> wrote:
>建议任何人?
使用std :: string你会好得多。
>Suggestions anyone?
You''d be much better off using std::string.
好的......也许是这样,但我真的不知道为什么?
Okay... maybe so, but I don''t really se why?
仅仅因为std :: string是'标准'。使用它可以让你的代码更好地重新用于初学者。
std :: string是否有一个共享的空字符串或者它可能使用NULL作为空的
串?
这将是一个实现细节。
如果std :: string也分配了一个空字符串,如果没有初始化,那么我看到
没有理由什么如此使用它...
一个体面的std :: string实现将确保''std :: string s;''是
_really_便宜。在这种情况下我不希望任何动态分配。
顺便说一句,我也写了我自己的CBL_ArrayList作为向量的等价物...
这让我觉得比一个''新''字符串类更糟糕的想法
(可能因某些编码相关的原因而有其用途)
ony差异是能力如果设置了标志,则自动缩小。
由于某些奇怪的原因,它在我的插入/删除基准测试中也超过了矢量,可能只是一堆设置或其他东西。
很高兴看到一些最小的代码证明这些结果。
除非你放弃一些''重要''标准,否则不容易相信:: vector
特征。
我肯定这一切也在Boost中(或者可能有的C ++扩展)但是我已经我学到了很多东西。
Simply because std::string is ''the standard''. Using it makes your code
better re-usable for starters.
Does std::string have a shared empty string or maybe it uses NULL as an
empty string?
This would be an implementation detail.
If std::string also allocated an empty string if not initialized, then I see
no reason what so ever to use it...
A decent std::string implementation will make sure that ''std::string s;'' is
_really_ cheap. I would not expect any dynamic allocation in this case.
BTW, I''ve also written my own CBL_ArrayList as an equivalent to vector...
This strikes me as an even worse idea than having a ''new'' string class
(which might have its uses for certain encoding related reasons)
ony differenceis the ability to automatically shrink if a flag is set.
For some odd reason it also outperformes vector in my insert/remove
benchmarks, probably just a bunch of settings or something.
Would be nice to see some minimal code demonstrating these results.
It''s not easy to believe unless you dropped some ''important'' std::vector
characteristics.
I''m sure all this is also in Boost (or whatever C++ extentions there might
be) but I''ve learned so much be writing it my self.
学习总是重写某些标准课程的好理由。
很少尽管如此,_using_他们的理由足够了;-)
Andre''[谁也不是特别喜欢std :: string但是使用它
$ b然而,$ b]
Learning is always a good reason to re-write certain standard classes.
It is rarely a good enough reason for _using_ them, though ;-)
Andre'' [who isn''t particularly fond of std::string either but uses it
nevertheless]
这篇关于哪里放一个常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!