Another way to handle array initialization
2008-03-08 12:30:16 来源:WEB开发网核心提示:PROBLEM: bwh@kato.prl.ufl.edu (Brian Hook) [...] I STILL haven't gotten a good technical reason why parameters to constrUCtors for arrays haven't been a
PROBLEM: bwh@kato.prl.ufl.edu (Brian Hook)
[...]
I STILL haven't gotten a good technical reason why
parameters to constrUCtors for arrays haven't been at least talked out (I'm
not a committee member, so I don't know if this has been shot down
informally, so if it has, forgive my lack of knowledge on this).
I even asked The Man himself, and his response was basically that he didn't
see much need for it. I know I run into instances on occasion where I want
to specify something that is constant over an entire array of objects. For
example, something even as simple as "name".
Object array1[100]("Member of array1");
Object array2[100]("Member of array2");
I suppose templates serve much the same purpose, but it just seems so
damned inconsistent to not have parameters to constructors for arrays!
Sure, there's the old trick:
Object::name = "Member of array1";
Object array[100];
Object::name = "Member of array2";
etc. etc.
Now if THAT ain't ugly, I don't know what is!
RESPONSE: kanze@us-es.sel.de (James Kanze), 6 Jul 94
The trick I use is slightly different:
template< double r , double i >
class ComplexWithInit : public Complex
{
public :
ComplexWithInit()
: Complex( r , i ) {}
} ;
Complex* c1 = new ComplexWithInit< 1.0 , 2.0 >[ 10 ] ;
Complex* c2 = new ComplexWithInit< 3.0 , 4.0 >[ 20 ] ;
Formally speaking, this is *not* guaranteed to work. Practically, I
would be interested in hearing about a plausible implementation where
it will not actually work. (Finding the correct standardese which
would permit guaranteeing this to work without causing problems
elsewhere is a non-trivial problem, however.)
- ››TOscilloscope 仿Windows任务管理器CPU使用记录组...
- ››tomcat不支持TCP/IP6协议
- ››tomcat 下的 url 大小写问题
- ››tomcat6.0.28 内存溢出PermGen Space
- ››Tomcat 系统架构与设计模式,第 2 部分: 设计模式...
- ››Tomcat 系统架构与设计模式,第 1 部分: 工作原理...
- ››TOMCAT和IIS整合
- ››Tomcat性能调优方案
- ››Tomcat6 下 MySQL 5.1 数据源配制
- ››Tomcat启动分析server.xml
- ››Tomcat 的过滤诀窍
- ››Tomcat DBCP配置方法
更多精彩
赞助商链接