Thread: Vectors

  1. #16
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Guess I'll have to rethink most of my coding. Hopefully this will give me less headaches Thanks for the help

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No one said C++ design was easy
    Good luck!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Dec 2006
    Posts
    51

    Still confused...

    Well I didn't think I should start a new thread since this thread caused all the problems, but I am having trouble figuring out how to manage the vector.

    If I create a separate class called Engine or something that basically holds the vector then runs the state thats on top of the vector I have no way of telling Engine to push back a new class. (Maybe if I use friend that way it might be able to access it or static? Not sure which one is better).

    Also isn't it a bad thing if I create a class for only one object?

    Basically I am clueless on how to update the vector since my objects function(MENU_State.update etc.) are the ones that tell me what I should add/pop. Any hints or suggestions?

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You wouldn't pass the contents of the vector around; you'd pass the Engine object around (by reference, probably). Each update or whatever can read the top one; if it needs to push, it's got the Engine right there ready.

    Edit: And I think the point of the Engine class was that it would be "the state of play": it would contain (a vector of) players, and (a vector of) game states, and (a vector of) inventory, and (a vector of) enemies, or whatever you have in your game there.
    Last edited by tabstop; 10-05-2008 at 08:49 PM.

  5. #20
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    But if I pass around the Engine class by reference wouldn't that defeat the purpose of not passing around the vector by reference?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The idea behind not passing the vector by reference was not that it couldn't be done, just that it wasn't (or might not be, I mean it's your game) a good design choice.

    Edit: I mean, passing a vector by reference:
    Code:
    void process_vector(std::vector<int> &bunch_of_ints) {
        int total = 0;
        while (total < 100 && !(bunch_of_ints.empty()) {
            total += bunch_of_ints.back();
            bunch_of_ints.pop_back();
        }
    }
    Rather contrived, but you get the idea.
    Last edited by tabstop; 10-05-2008 at 09:42 PM.

  7. #22
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    So should I do it like this?

    Main
    Code:
    Engine Engine
    ...
    Loop
    Message pump
    Engine.run();
    ....
    Engine
    Code:
    public:
    vector  states=new state;
    run(){
    States.back()->run(&this or &Engine (Not sure which));
    }
    States
    Code:
    if[key=pressed]{
    Engine.vector.pushback new state
    }
    Something like that?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Without knowing what you're doing in run() I can't be very specific. But I wouldn't see why Engine.run would need to call a different run function. You're already running, just do the thing already.

  9. #24
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Well the run function for my states runs the update function then the render function in order. I guess I could run it from the engine and save some memory and then I only have to pass the engine object once because update only needs it.

    btw instead of passing a reference each time update is called shouldn't I just have a reference object(Not sure if I could do this) in my state class and on creation I just refrence the object(thus reducing the overhead for function calls).

    So this the revised pseudo code

    Main
    Code:
    Engine Engine
    ...
    Loop
    Message pump
    Engine.run();
    ....
    Engine
    Code:
    public:
    vector  states=new state;
    run(){
    States.back()->update(&this or &engine [not sure which is correct  yet]);
    //Might not even need to pass it if I could just put it in the state.
    States.back()->render();
    }
    States
    Code:
    if[key=pressed]{
    Engine.vector.pushback new state(&this or &engine [not sure which is correct  yet]);
    //This way the new state would have access to the engine without needing to constantly get a new reference to it.
    }
    Is that better?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM