Thread: Object Creation at Run Time

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    Question Object Creation at Run Time

    Ok, I am writing a simple arcade style game to strengthen my programming skills. The game has a character that is created at the start of each level. Lets say its level 2, the game will create 4 "Boblies", so double what level it is. What is the best way to create and manage these objects? The only thing these objects need to do is calculate were its going to move to next, and maby draw it self(still undecided if the Boblie class should do this, or if the GameCore class should.)

    Someone suggested that I use <vector>, and I thought about using linked lists. But I herd that vectors are slow, and linked lists are too complex to get specific info from a object. Any ideas

  2. #2
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    Both are good choices and would both work fine for what you are trying to do. I'm not sure about vectors being slow but I have heard that too. However, in a relatively simple arcade game there aren't going to be any insane graphic effects and whatnot that will eat your fps, so a vector won't kill you if it is slower. Linked lists can be a little complicated, however they would probably work fine for what you are trying to do.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Vectors are almost as fast as normal arrays (I say almost, because there is a little difference in time but it is very very small).
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could use some other kind of STL container if you don't want to use linked lists.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    A vector should be as fast as an array except for at creation. Also resizing the vector will cause a performance hit. If you know how many objects your are going to have ahead of time you can use the .reserve(size_t count) funtion of the container class.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use std::list as it has way better adding/removing time complexity than std::vector. The advantage of std::vector is that you have random access but if removing elements the indices would be changed anyway.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    just make the class of what ever object you are going to want to make multiple instances of. then make another class that holds these instances. use vectors in this class so that it is resizable and easy to implement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert return type of function at run time ?
    By vinod_mrd in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2008, 11:49 AM
  2. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  3. How program run predefine time?
    By Arafat_211184 in forum C Programming
    Replies: 9
    Last Post: 12-16-2005, 03:44 AM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM