Menu

dynamically allocated instances

Kurgusov
2007-09-29
2012-09-26
  • Kurgusov

    Kurgusov - 2007-09-29

    Hello everyone,
    I hope your all well.

    I want to dynamically allocate the required amount of instances of the class character{...};
    after the programs entry point so that I can represent the total amount of players (characters) in the game,the total amount being loaded in from a text file thus the reason for dynamic allocation..

    But how do I tell my header files that.....

    1]The new instances will exist even though they have not been declared at compile time?
    I can't use the name of the dynamically allocated instance in any of the header or .cpp files cause they (the instances) dont exist yet.

    Do I need to use a pointer to the class?
    Or work out how to pass in command line arguments?

    (secondary thoughts)
    Ive been told that the CodeBlocks IDE nightly builds are very good.
    Im torn between wxDev-cpp and trying CodeBlocks,do you think that wxDev-cpp is
    better?

     
    • Wayne Keen

      Wayne Keen - 2007-09-29

      Answering your second question. Try them both! You can have them installed
      side by side.

      Wayne

       
    • Anonymous

      Anonymous - 2007-09-29

      class cExample
      {
      ...
      int member ;
      ...
      }

      // Declare a pointer to the array of dynamic instances (pointer-to-pointer)
      class cExample** objets ;

      // Determine how many instances to create (however you like - just a contrived example)
      int count = getNumberOfInstances() ;

      // Instantiate an array of 'count' instances.
      objects = new cExample[count] ;

      // Access an element in the array.
      objects[n]->member = x ;

      wxDev-C++ is Dev-C++ with GUI designer and GUI class library. Code::Blocks like Dev-C++ is simply an IDE with few frills. That is to say they address different problems. If Visual GUI development is what you want Code::Blocks will not help.

      Clifford

       
    • Anonymous

      Anonymous - 2007-09-29

      ... w.r.t. command line arguments, you could just Google it, but if your program were invoked thus:

      myprog 10

      with the intent of creating ten instances:

      int main( int argc, char** argv )
      {
      int count = 0 ;

      if( argc > 1 )
      {
      count = atoi( argv[1] )
      }

      if( count > 0 )
      {
      objects = new cExample[count] ;
      ...
      }
      ...
      }

      Clifford

       
    • Kurgusov

      Kurgusov - 2007-09-30

      Thanks alot for the examples Clifford.

      Now I can do it,yipee.

       
    • Kurgusov

      Kurgusov - 2007-09-30

      Spoke a little too early.

      In your example Clifford that I used the compiler tells me that...

      class cExample** object ;

      object = new cExample[ count ] ;

      .....cannot convert patrons to patrons* in assignment.

      any chance of some feed back?

       
    • Kurgusov

      Kurgusov - 2007-09-30

      sorry,it says...

      cannot convert cExample to cExample* in assignment.

       
    • Anonymous

      Anonymous - 2007-09-30

      Sorry:

      // Declare a pointer to the array of dynamic instances (pointer-to-pointer)
      class cExample* objects ;

      They do say "there is no problem that cannot be solved with one more level of indirection", here you needed one less! ;-)

      Clifford

       
    • Kurgusov

      Kurgusov - 2007-09-30

      okay,
      I know your the one that knows what he's doing,
      but I get a new error with one level of indirection.

      heres the test code...

      //all on main source file

      class cExample{
      public:
      cExample(){} ;
      ~cExample(){} ;

          int data ;
      };
      

      int main()
      {
      class cExample* objects ;
      int count = 5 ;

      objects = new cExample[ count ] ;
      
      objects[0] -> data = 17 ;
      
      std::cout<< objects[0] -> data ;
      std::cout<< "\n" ;
      
      std::cin.getline() ;
      return 0 ;
      

      }

      .....In function main(int, char**)
      base operand of '->' has non-pointer type 'cExample'
      base operand of '->' has non-pointer type 'cExample'

      [build error][test.o]Error 1

       
    • Anonymous

      Anonymous - 2007-09-30

      No at all, today it seems that I don't know what I am doing. Each element in teh array is an actual insrtance of cExample, not a pointer, so it should be:

      objects[0].data = 17 ;

      std::cout << objects[0].data ;

      Which is exactly what the compiler was telling you.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.