Thread: dynamic object initialisation??

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    6

    dynamic object initialisation??

    Hi all,
    just a general question about c++..
    I am new to c++ programming and need to create instances of objects at runtime. However, the number of instances to be initialised varies each time the exe is run, and is dependant on variables passed to the exe. pretty much any number of instances (from say 1 to 30) could be required each time.

    As far as i am aware, i cannot declare instances "on the fly", they must be fixed for each time the exe runs.

    I am fairly sure there must be techniques that can be employed to solve this type of problem, since i imagine it is quite a common one. But because of my lack of experience in c++ i am not sure which direction to take.

    any advice on this would be gratefully recieved.

    Bob

  2. #2
    Unregistered
    Guest

    Lightbulb Not hard at all

    Code:
    class CBob {
      bool bBobIsCool;
    public:
      CBob() : bBobIsCool(false) { }
      bool MakeMeACoolGuy() { bBobIsCool = true; }
    };
    
    //...
    
    int nNumObjects = atoi (argv[1]);
    
    CBob *pBobbers = new CBob[nNumObjects];
    
    for (int i=0;i<nNumObjects;++i)
      pBobbers[i]->MakeMeACoolGuy();
    
    delete [] pBobbers;
    There ya go

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    6
    splendid

    thanks.


    bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  2. Finding object code
    By kidburla in forum C Programming
    Replies: 3
    Last Post: 11-29-2005, 01:09 PM
  3. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  4. Replies: 6
    Last Post: 10-30-2002, 09:03 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM