instantiate an array of class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony

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


  • wittempj@hotmail.com

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

    Comment

    • Pete Becker

      #3
      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)

      Comment

      • Jay Nabonne

        #4
        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

        Comment

        • Ron Natalie

          #5
          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.

          Comment

          • Pete Becker

            #6
            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)

            Comment

            • Farhan

              #7
              Re: instantiate an array of class

              I think his code is just fine.

              "Pete Becker" <petebecker@acm .org> wrote in message
              news:H_GdnWVt36 PCkQPfRVn-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]


              Comment

              Working...