instantiate an array of class

A

Anthony

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.num << endl;

return rval;
}
 
W

wittempj

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

Pete Becker

Anthony said:
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.

You do it like this:
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.num << endl;

return rval;
}


What's the problem?
 
J

Jay Nabonne

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;

// You can do:
foo f[5] = {foo(1), foo(2), foo(3), foo(4), foo(5)};
for (int i = 0; i < sizeof(f) / sizeof(*f); i++)
cout << f.num << endl;

return rval;
}


It's helpful if the constructor takes multiple arguments.

- Jay
 
R

Ron Natalie

Pete said:
Anthony said:
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.


You do it like this:
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.num << endl;

return rval;
}


What's the problem?

He forgot the semicolon after the class definition.
 
P

Pete Becker

Ron said:
He forgot the semicolon after the class definition.

Well, yes, but is that really what he's complaining about, or is it a
typo in the posted code?
 
F

Farhan

I think his code is just fine.

Pete Becker said:
Anthony said:
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.

You do it like this:
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.num << endl;

return rval;
}


What's the problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top