Will it work?

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

    Will it work?

    Hi:

    I want all objects of a class share the same set of data, which will
    be input from a file when the code start to run. In my design, I use
    static data member.
    The definition of the class is as below:

    class myclass{
    public:
    static int data[100]; //The shared data.
    ifstream infile( "datafile.t xt", ios::in );
    int i = 0;
    while ( infile >> data[i] ) ++i; //Input data from the file.

    //Definition of other data member and member functions.
    void funct1();
    void funct2();
    char name[10];

    protected:
    .....
    }

    When the code runs, the data will be read in from the file. The
    following objects:

    myclass *c_1 = new myclass;
    myclass *c_2 = new myclass;
    .....
    myclass *c_100 = new myclass;

    c_1, c_2,..., c_100 share the same data.

    Will my design work? If possible, please give me your suggestion.
    By the way, this is not a homework question. :-) I am working on a
    project and considering how to share data.

    Thanks a lot.

    John
  • Victor Bazarov

    #2
    Re: Will it work?

    "John" <johnw822003@ya hoo.com> wrote...[color=blue]
    > I want all objects of a class share the same set of data, which will
    > be input from a file when the code start to run. In my design, I use
    > static data member.
    > The definition of the class is as below:
    >
    > class myclass{
    > public:
    > static int data[100]; //The shared data.
    > ifstream infile( "datafile.t xt", ios::in );
    > int i = 0;
    > while ( infile >> data[i] ) ++i; //Input data from the file.[/color]


    This is not C++. You cannot have executable code in the class
    definition unless it's in a function definition. Besides, you
    might want to look into

    while (infile && (i < 100))
    infile >> data[i++];

    But, again, not directly in the class definition.
    [color=blue]
    >
    > //Definition of other data member and member functions.
    > void funct1();
    > void funct2();
    > char name[10];
    >
    > protected:
    > .....
    > }
    >
    > When the code runs, the data will be read in from the file. The
    > following objects:
    >
    > myclass *c_1 = new myclass;
    > myclass *c_2 = new myclass;
    > .....
    > myclass *c_100 = new myclass;
    >
    > c_1, c_2,..., c_100 share the same data.
    >
    > Will my design work?[/color]

    Not likely. You should look into a static data member which,
    when initialised, will fill in the 'data' array.
    [color=blue]
    > If possible, please give me your suggestion.
    > By the way, this is not a homework question. :-) I am working on a
    > project and considering how to share data.[/color]

    OK

    Victor


    Comment

    • Thomas Matthews

      #3
      Re: Will it work?

      John wrote:[color=blue]
      > Hi:
      >[/color]
      [snip]
      [color=blue]
      > When the code runs, the data will be read in from the file. The
      > following objects:
      >
      > myclass *c_1 = new myclass;
      > myclass *c_2 = new myclass;
      > .....
      > myclass *c_100 = new myclass;[/color]

      I don't understand why you are using dynamic allocation.
      Is this a leftover from Java?

      If you have a lot of instances, you should use a container class
      such as vector, list, set, map, etc.

      With a vector:
      myclass * p_new_class;
      std::vector<myc lass *> container;

      //...
      p_new_class = new myclass;
      container.push_ back(p_new_clas s);
      // ...
      std::vector<myc lass *>::const_itera tor c_iter;
      for (c_iter = container.begin ();
      c_iter != container.end() ;
      ++c_iter)
      {
      // process an element
      cout << (*iter) << "\n";
      }

      [color=blue]
      >
      > c_1, c_2,..., c_100 share the same data.[/color]

      [color=blue]
      >
      > Will my design work? If possible, please give me your suggestion.
      > By the way, this is not a homework question. :-) I am working on a
      > project and considering how to share data.
      >
      > Thanks a lot.
      >
      > John[/color]


      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      • John

        #4
        Re: Will it work?

        I got a new design. Will it work?

        class myclass{
        public:
        void myclass();
        int data[100]; //The shared data.
        void funct1();
        void funct2();
        char name[10];

        protected:
        .....
        }

        void myclass::myclas s(){
        ifstream infile( "datafile.t xt", ios::in );
        int i = 0;
        while (infile && (i < 100))
        infile >> data[i++]; //Input data from the file.
        }


        When the code runs, the data will be read in from the file. The
        following objects:

        myclass *c_1 = new myclass;
        myclass *c_2 = new myclass;
        .....
        myclass *c_100 = new myclass;

        c_1, c_2,..., c_100 read from the same file and share the same data.



        "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:<BTdQb.108 909$nt4.406718@ attbi_s51>...[color=blue]
        > "John" <johnw822003@ya hoo.com> wrote...[color=green]
        > > I want all objects of a class share the same set of data, which will
        > > be input from a file when the code start to run. In my design, I use
        > > static data member.
        > > The definition of the class is as below:
        > >
        > > class myclass{
        > > public:
        > > static int data[100]; //The shared data.
        > > ifstream infile( "datafile.t xt", ios::in );
        > > int i = 0;
        > > while ( infile >> data[i] ) ++i; //Input data from the file.[/color]
        >
        >
        > This is not C++. You cannot have executable code in the class
        > definition unless it's in a function definition. Besides, you
        > might want to look into
        >
        > while (infile && (i < 100))
        > infile >> data[i++];
        >
        > But, again, not directly in the class definition.
        >[color=green]
        > >
        > > //Definition of other data member and member functions.
        > > void funct1();
        > > void funct2();
        > > char name[10];
        > >
        > > protected:
        > > .....
        > > }
        > >
        > > When the code runs, the data will be read in from the file. The
        > > following objects:
        > >
        > > myclass *c_1 = new myclass;
        > > myclass *c_2 = new myclass;
        > > .....
        > > myclass *c_100 = new myclass;
        > >
        > > c_1, c_2,..., c_100 share the same data.
        > >
        > > Will my design work?[/color]
        >
        > Not likely. You should look into a static data member which,
        > when initialised, will fill in the 'data' array.
        >[color=green]
        > > If possible, please give me your suggestion.
        > > By the way, this is not a homework question. :-) I am working on a
        > > project and considering how to share data.[/color]
        >
        > OK
        >
        > Victor[/color]

        Comment

        • Victor Bazarov

          #5
          Re: Will it work?

          "John" <johnw822003@ya hoo.com> wrote...[color=blue]
          > I got a new design. Will it work?[/color]

          Yes, but at what price!
          [color=blue]
          >
          > class myclass{
          > public:
          > void myclass();
          > int data[100]; //The shared data.
          > void funct1();
          > void funct2();
          > char name[10];
          >
          > protected:
          > .....
          > }
          >
          > void myclass::myclas s(){[/color]

          So, you put the file reading into the constructor...
          [color=blue]
          > ifstream infile( "datafile.t xt", ios::in );
          > int i = 0;
          > while (infile && (i < 100))
          > infile >> data[i++]; //Input data from the file.
          > }
          >
          >
          > When the code runs, the data will be read in from the file. The
          > following objects:
          >
          > myclass *c_1 = new myclass;
          > myclass *c_2 = new myclass;
          > .....
          > myclass *c_100 = new myclass;
          >
          > c_1, c_2,..., c_100 read from the same file and share the same data.[/color]

          And since they are constructed 100 times, the file will be opened
          and read 100 times. WHY? WHAT FOR?

          Create a static function "initData" and a static int (just for the heck
          of it). Then when initialising the static int, call the 'initData'
          function. This way you will only initialise the array ONCE, when the
          static int is initialised.

          Do I need spell it out in C++ or can you handle it yourself?
          [color=blue]
          >
          >
          >
          > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message[/color]
          news:<BTdQb.108 909$nt4.406718@ attbi_s51>...[color=blue][color=green]
          > > "John" <johnw822003@ya hoo.com> wrote...[color=darkred]
          > > > I want all objects of a class share the same set of data, which will
          > > > be input from a file when the code start to run. In my design, I use
          > > > static data member.
          > > > The definition of the class is as below:
          > > >
          > > > class myclass{
          > > > public:
          > > > static int data[100]; //The shared data.
          > > > ifstream infile( "datafile.t xt", ios::in );
          > > > int i = 0;
          > > > while ( infile >> data[i] ) ++i; //Input data from the file.[/color]
          > >
          > >
          > > This is not C++. You cannot have executable code in the class
          > > definition unless it's in a function definition. Besides, you
          > > might want to look into
          > >
          > > while (infile && (i < 100))
          > > infile >> data[i++];
          > >
          > > But, again, not directly in the class definition.
          > >[color=darkred]
          > > >
          > > > //Definition of other data member and member functions.
          > > > void funct1();
          > > > void funct2();
          > > > char name[10];
          > > >
          > > > protected:
          > > > .....
          > > > }
          > > >
          > > > When the code runs, the data will be read in from the file. The
          > > > following objects:
          > > >
          > > > myclass *c_1 = new myclass;
          > > > myclass *c_2 = new myclass;
          > > > .....
          > > > myclass *c_100 = new myclass;
          > > >
          > > > c_1, c_2,..., c_100 share the same data.
          > > >
          > > > Will my design work?[/color]
          > >
          > > Not likely. You should look into a static data member which,
          > > when initialised, will fill in the 'data' array.
          > >[color=darkred]
          > > > If possible, please give me your suggestion.
          > > > By the way, this is not a homework question. :-) I am working on a
          > > > project and considering how to share data.[/color]
          > >
          > > OK
          > >
          > > Victor[/color][/color]


          Comment

          • John

            #6
            Re: Will it work?

            "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:<nflQb.137 092$na.216154@a ttbi_s04>...[color=blue]
            > "John" <johnw822003@ya hoo.com> wrote...[color=green]
            > > I got a new design. Will it work?[/color]
            >
            > Yes, but at what price!
            >[color=green]
            > >
            > > class myclass{
            > > public:
            > > void myclass();
            > > int data[100]; //The shared data.
            > > void funct1();
            > > void funct2();
            > > char name[10];
            > >
            > > protected:
            > > .....
            > > }
            > >
            > > void myclass::myclas s(){[/color]
            >
            > So, you put the file reading into the constructor...
            >[color=green]
            > > ifstream infile( "datafile.t xt", ios::in );
            > > int i = 0;
            > > while (infile && (i < 100))
            > > infile >> data[i++]; //Input data from the file.
            > > }
            > >
            > >
            > > When the code runs, the data will be read in from the file. The
            > > following objects:
            > >
            > > myclass *c_1 = new myclass;
            > > myclass *c_2 = new myclass;
            > > .....
            > > myclass *c_100 = new myclass;
            > >
            > > c_1, c_2,..., c_100 read from the same file and share the same data.[/color]
            >
            > And since they are constructed 100 times, the file will be opened
            > and read 100 times. WHY? WHAT FOR?
            >
            > Create a static function "initData" and a static int (just for the heck
            > of it). Then when initialising the static int, call the 'initData'
            > function. This way you will only initialise the array ONCE, when the
            > static int is initialised.
            >
            > Do I need spell it out in C++ or can you handle it yourself?
            >[/color]

            Sorry, I can not figure it out. I just start to learn C++.
            When to access the file and read data? Could you please spell it out? :-)

            John
            [color=blue][color=green]
            > >
            > >
            > >
            > > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message[/color]
            > news:<BTdQb.108 909$nt4.406718@ attbi_s51>...[color=green][color=darkred]
            > > > "John" <johnw822003@ya hoo.com> wrote...
            > > > > I want all objects of a class share the same set of data, which will
            > > > > be input from a file when the code start to run. In my design, I use
            > > > > static data member.
            > > > > The definition of the class is as below:
            > > > >
            > > > > class myclass{
            > > > > public:
            > > > > static int data[100]; //The shared data.
            > > > > ifstream infile( "datafile.t xt", ios::in );
            > > > > int i = 0;
            > > > > while ( infile >> data[i] ) ++i; //Input data from the file.
            > > >
            > > >
            > > > This is not C++. You cannot have executable code in the class
            > > > definition unless it's in a function definition. Besides, you
            > > > might want to look into
            > > >
            > > > while (infile && (i < 100))
            > > > infile >> data[i++];
            > > >
            > > > But, again, not directly in the class definition.
            > > >
            > > > >
            > > > > //Definition of other data member and member functions.
            > > > > void funct1();
            > > > > void funct2();
            > > > > char name[10];
            > > > >
            > > > > protected:
            > > > > .....
            > > > > }
            > > > >
            > > > > When the code runs, the data will be read in from the file. The
            > > > > following objects:
            > > > >
            > > > > myclass *c_1 = new myclass;
            > > > > myclass *c_2 = new myclass;
            > > > > .....
            > > > > myclass *c_100 = new myclass;
            > > > >
            > > > > c_1, c_2,..., c_100 share the same data.
            > > > >
            > > > > Will my design work?
            > > >
            > > > Not likely. You should look into a static data member which,
            > > > when initialised, will fill in the 'data' array.
            > > >
            > > > > If possible, please give me your suggestion.
            > > > > By the way, this is not a homework question. :-) I am working on a
            > > > > project and considering how to share data.
            > > >
            > > > OK
            > > >
            > > > Victor[/color][/color][/color]

            Comment

            • Victor Bazarov

              #7
              Re: Will it work?

              "John" <johnw822003@ya hoo.com> wrote...[color=blue]
              > [...]
              > Sorry, I can not figure it out. I just start to learn C++.
              > When to access the file and read data? Could you please spell it out? :-)[/color]

              class A {
              static int data[10000000];
              static int dummy;
              static int init_data() {
              // open the file
              // read the contents into 'data' array
              return 42;
              }
              public:
              A() { // normal constructor, no 'data' manipulation
              // blah blah
              }
              };

              int A::dummy = A::init_data(); // 'dummy' initialised only once,
              // so, file is only read once
              // and millions of 'data' elements
              // are only assigned once
              // not every time an A is created

              Victor


              Comment

              • John

                #8
                Re: Will it work?

                Hi Victor:

                Thanks a lot. I got the trick.

                John

                "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:<6NCQb.140 281$I06.1291768 @attbi_s01>...[color=blue]
                > "John" <johnw822003@ya hoo.com> wrote...[color=green]
                > > [...]
                > > Sorry, I can not figure it out. I just start to learn C++.
                > > When to access the file and read data? Could you please spell it out? :-)[/color]
                >
                > class A {
                > static int data[10000000];
                > static int dummy;
                > static int init_data() {
                > // open the file
                > // read the contents into 'data' array
                > return 42;
                > }
                > public:
                > A() { // normal constructor, no 'data' manipulation
                > // blah blah
                > }
                > };
                >
                > int A::dummy = A::init_data(); // 'dummy' initialised only once,
                > // so, file is only read once
                > // and millions of 'data' elements
                > // are only assigned once
                > // not every time an A is created
                >
                > Victor[/color]

                Comment

                Working...