instantiate an array of class 
July 23rd, 2005, 05:58 AM
| | | |
Hello All,
Say I have a class foo, how can I instantiate say 5 of them and store
information about each in an array?
I am open to any other approach that may be useful. Thanks, help is
appreciated.
-Anthony
class foo
{
public:
int num;
foo(int val) : num(val) { }
}
int main (int argc, char ** argv)
{
int rval = 0;
foo f[5] = {1, 2, 3, 4 , 5};
for (int i = 0; i < sizeof(f) / sizeof(*f); i++)
cout << f[i].num << endl;
return rval;
} | 
July 23rd, 2005, 05:58 AM
| | | | re: instantiate an array of class
You could use a vector like:
#include <vector>
using namespace std;
class foo
{
public:
int num;
foo(int val) : num(val) { }
};
int main (int argc, char ** argv)
{
int rval = 0;
vector<foo> f;
for(int i = 0; i < 5; ++i)
{
foo x(i);
f.push_back(x);
}
return rval;
} | 
July 23rd, 2005, 05:58 AM
| | | | re: instantiate an array of class
Anthony wrote:[color=blue]
>
> Say I have a class foo, how can I instantiate say 5 of them and store
> information about each in an array?
> I am open to any other approach that may be useful. Thanks, help is
> appreciated.[/color]
You do it like this:
[color=blue]
>
> class foo
> {
> public:
> int num;
> foo(int val) : num(val) { }
> }
>
> int main (int argc, char ** argv)
> {
> int rval = 0;
> foo f[5] = {1, 2, 3, 4 , 5};
>
> for (int i = 0; i < sizeof(f) / sizeof(*f); i++)
> cout << f[i].num << endl;
>
> return rval;
> }
>
>[/color]
What's the problem?
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com) | 
July 23rd, 2005, 05:58 AM
| | | | re: instantiate an array of class
On Wed, 01 Jun 2005 19:01:59 +0000, Anthony wrote:
[color=blue]
> Hello All,
>
> Say I have a class foo, how can I instantiate say 5 of them and store
> information about each in an array?
> I am open to any other approach that may be useful. Thanks, help is
> appreciated.
> -Anthony
>
> class foo
> {
> public:
> int num;
> foo(int val) : num(val) { }
> }
>
> int main (int argc, char ** argv)
> {
> int rval = 0;[/color]
// You can do:
foo f[5] = {foo(1), foo(2), foo(3), foo(4), foo(5)};
[color=blue]
>
> for (int i = 0; i < sizeof(f) / sizeof(*f); i++)
> cout << f[i].num << endl;
>
> return rval;
> }[/color]
It's helpful if the constructor takes multiple arguments.
- Jay | 
July 23rd, 2005, 05:58 AM
| | | | re: instantiate an array of class
Pete Becker wrote:[color=blue]
> Anthony wrote:
>[color=green]
>>
>> Say I have a class foo, how can I instantiate say 5 of them and store
>> information about each in an array?
>> I am open to any other approach that may be useful. Thanks, help is
>> appreciated.[/color]
>
>
> You do it like this:
>[color=green]
>>
>> class foo
>> {
>> public:
>> int num;
>> foo(int val) : num(val) { }
>> }
>>
>> int main (int argc, char ** argv)
>> {
>> int rval = 0;
>> foo f[5] = {1, 2, 3, 4 , 5};
>>
>> for (int i = 0; i < sizeof(f) / sizeof(*f); i++)
>> cout << f[i].num << endl;
>>
>> return rval;
>> }
>>
>>[/color]
>
> What's the problem?
>[/color]
He forgot the semicolon after the class definition. | 
July 23rd, 2005, 05:58 AM
| | | | re: instantiate an array of class
Ron Natalie wrote:
[color=blue][color=green]
>>[/color]
> He forgot the semicolon after the class definition.[/color]
Well, yes, but is that really what he's complaining about, or is it a
typo in the posted code?
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com) | 
July 23rd, 2005, 05:59 AM
| | | | re: instantiate an array of class
I think his code is just fine.
"Pete Becker" <petebecker@acm.org> wrote in message
news:H_GdnWVt36PCkQPfRVn-qg@rcn.net...[color=blue]
> Anthony wrote:[color=green]
> >
> > Say I have a class foo, how can I instantiate say 5 of them and store
> > information about each in an array?
> > I am open to any other approach that may be useful. Thanks, help is
> > appreciated.[/color]
>
> You do it like this:
>[color=green]
> >
> > class foo
> > {
> > public:
> > int num;
> > foo(int val) : num(val) { }
> > }
> >
> > int main (int argc, char ** argv)
> > {
> > int rval = 0;
> > foo f[5] = {1, 2, 3, 4 , 5};
> >
> > for (int i = 0; i < sizeof(f) / sizeof(*f); i++)
> > cout << f[i].num << endl;
> >
> > return rval;
> > }
> >
> >[/color]
>
> What's the problem?
>
> --
>
> Pete Becker
> Dinkumware, Ltd. ( http://www.dinkumware.com)[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,714 network members.
|