🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Constructor with an array problem--Can you help?

Started by
1 comment, last by illuna 24 years, 7 months ago
The reason that second way isn't legal is because nothing but initialization of variables is allowed with that little colon thing. For loops aren't allowed, and neither are array indices.

Why can't you just initialize this array inside the constructor? E.g.

MyConst::MyConst(MyParams) {
for(int i=0;i<whatever;i++) {
MyVar = MyParams;<BR> }<BR>}<BR></code><P>Jonathan<p>[This message has been edited by Jonathan (edited November 18, 1999).]

Advertisement
In Visual C++,
This is legal (MyConst is a constructor, I call MyVar, MyVar2 and MyVar3 constructors with MyParams I receive in MyConst)

MyConst::MyConst(MyParams):
MyVar(MyParams),
MyVar2(MyParams),
MyVar3(MyParams{
}
------------------
This is not legal.

MyConst::MyConst(MyParams):
for (int i=0,i<3;i++) MyVar(MyParams){<BR>}<P>Why????? Or at least if this would work (but it don't)<P>MyConst::MyConst(MyParams):<BR> MyVar[0](MyParams),<BR> MyVar[1](MyParams),<BR> MyVar[2](MyParams{<BR>}<P><BR>I really DO need to initailize an array this way in a constructor. I don't know how so I have to do just like my first example with my eight variable. Pain in the neck. Anyone can tell me how to handle an array in this case?<P>Thanks!<BR>Illuna<p>[This message has been edited by illuna (edited November 18, 1999).]

Instead of :

MyConst::MyConst(MyParams):
for (int i=0,i<3;i++) MyVar(MyParams){<BR>}<P>use<P>MyConst::MyConst(MyParams)<BR>{<BR>for (int i=0,i<3;i++) MyVar.init(MyParams)<BR>}<P>where MyVar[3] was already defined in the class header and init is an initialization function.<P>If you need to use the constructor (and it's probably a better way to do it anyway), make an array of myVar pointers (ie myvar* MyVar[3]) and then the constructor should be:<P>MyConst:MyConst(param)<BR>{<BR> for(int i = 0; i < 3; i++)<BR> {<BR> MyVar[3] = new myvar(param);<BR> }<BR>}<P>(you can't run actual code outside of the {}'s thats why your examples won't work.

-the logistical one-http://members.bellatlantic.net/~olsongt

This topic is closed to new replies.

Advertisement