Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

illuna

Constructor with an array problem--Can you help?

This topic is 6820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] = MyParams;
}
}

Jonathan

[This message has been edited by Jonathan (edited November 18, 1999).]

Share this post


Link to post
Share on other sites
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[i](MyParams){
}

Why????? Or at least if this would work (but it don't)

MyConst::MyConst(MyParams):
MyVar[0](MyParams),
MyVar[1](MyParams),
MyVar[2](MyParams{
}


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?

Thanks!
Illuna

[This message has been edited by illuna (edited November 18, 1999).]

Share this post


Link to post
Share on other sites
Instead of :

MyConst::MyConst(MyParams):
for (int i=0,i<3;i++) MyVar[i](MyParams){
}

use

MyConst::MyConst(MyParams)
{
for (int i=0,i<3;i++) MyVar[i].init(MyParams)
}

where MyVar[3] was already defined in the class header and init is an initialization function.

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:

MyConst:MyConst(param)
{
for(int i = 0; i < 3; i++)
{
MyVar[3] = new myvar(param);
}
}

(you can't run actual code outside of the {}'s thats why your examples won't work.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!