Connecting Tech Pros Worldwide Help | Site Map

instantiate an array of class

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:58 AM
Anthony
Guest
 
Posts: n/a
Default instantiate an array of class

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



  #2  
Old July 23rd, 2005, 04:58 AM
wittempj@hotmail.com
Guest
 
Posts: n/a
Default 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;
}

  #3  
Old July 23rd, 2005, 04:58 AM
Pete Becker
Guest
 
Posts: n/a
Default 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)
  #4  
Old July 23rd, 2005, 04:58 AM
Jay Nabonne
Guest
 
Posts: n/a
Default 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
  #5  
Old July 23rd, 2005, 04:58 AM
Ron Natalie
Guest
 
Posts: n/a
Default 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.
  #6  
Old July 23rd, 2005, 04:58 AM
Pete Becker
Guest
 
Posts: n/a
Default 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)
  #7  
Old July 23rd, 2005, 04:59 AM
Farhan
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,840 network members.