473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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;
}
Jul 23 '05 #1
6 1907
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;
}

Jul 23 '05 #2
Anthony wrote:

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

return rval;
}


What's the problem?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3
On Wed, 01 Jun 2005 19:01:59 +0000, Anthony wrote:
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[i].num << endl;

return rval;
}


It's helpful if the constructor takes multiple arguments.

- Jay
Jul 23 '05 #4
Pete Becker wrote:
Anthony wrote:

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

return rval;
}


What's the problem?

He forgot the semicolon after the class definition.
Jul 23 '05 #5
Ron Natalie wrote:

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?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6
I think his code is just fine.

"Pete Becker" <pe********@acm.org> wrote in message
news:H_********************@rcn.net...
Anthony wrote:

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

return rval;
}


What's the problem?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Colin Mc Mahon | last post by:
Hi all, I currently use a class to interface with my databases, allowing me to insert, update, delete and retrieve records from the database as methods of the class. I have now created a...
5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
1
by: Sharon | last post by:
I have a delegate definition: public delegate void SomeEventHandler(object obj); To make an instance of it it’s easy: public class myClass { public event SomeEventHandler handler; } But,...
2
by: Merk | last post by:
I'm wanting to know if/how it would be possible to load a form based on a string that contains the name of the form (class). For example, instead of doing this: myForm f = new myForm();...
2
by: Pyenos | last post by:
class model:pass class view: model() class controller: model() I can instantiate clsss model from inside class view but I can't instantiate class model from inside controller, due to the...
5
by: Pyenos | last post by:
class One: Two() #can't instantiate class Two: Three() #can't instantiate class Three:pass
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
4
by: Arne-Kolja Bachstein | last post by:
Hi there, I am planning to implement a plugin system, based on the observer pattern in some way, but now I am stuck with the instantiation of the plugins. I want the plugins to have their own...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.