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;
} 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;
}
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)
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
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.
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)
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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();
|
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,...
|
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();...
|
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...
|
by: Pyenos |
last post by:
class One:
Two() #can't instantiate
class Two:
Three() #can't instantiate
class Three:pass
|
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...
|
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...
|
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 ...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |