论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

Another way to handle array initialization

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:36:49

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.)
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058