Connecting Tech Pros Worldwide Forums | Help | Site Map

instantiate an array of class

Anthony
Guest
 
Posts: n/a
#1: Jul 23 '05
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;
}



wittempj@hotmail.com
Guest
 
Posts: n/a
#2: Jul 23 '05

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;
}

Pete Becker
Guest
 
Posts: n/a
#3: Jul 23 '05

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)
Jay Nabonne
Guest
 
Posts: n/a
#4: Jul 23 '05

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
Ron Natalie
Guest
 
Posts: n/a
#5: Jul 23 '05

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.
Pete Becker
Guest
 
Posts: n/a
#6: Jul 23 '05

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)
Farhan
Guest
 
Posts: n/a
#7: Jul 23 '05

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]


Closed Thread