Thread: Quick question - class declaration

  1. #1
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77

    Quick question - class declaration

    I'm working my way steadily into the C++ stuff and was wondering if the following is OK. (I'm making a clone of tertis for practice with SDL.)

    I have a class "Block" which will generate a random colour and create one of 5 random shapes.

    I am trying to make a "BlockGenerator" which will act as a list of all blocks on display, add blocks, deal with collisions etc.

    I was writing a function to add a block to the display:

    Here's an edited header file:

    Code:
    #include "Blocks.h"
    
    class BlockGenerator {
    public:
        static vector<Block *> blockList;
        int indexOfCurrentBlock;
        BlockGenerator();
        void addBlock();
    };
    And here's a snippet from the source file:

    Code:
    vector <Block *> BlockGenerator::blockList;
    
    BlockGenerator::BlockGenerator() {
    }
    
    void BlockGenerator::addBlock() {    
        Block newBlock;
        newBlock.onInit();
        BlockGenerator::blockList.push_back(&newBlock);
    }

    My question is about the last bit. Because I've declared a new instance of Block within the function, I've got this feeling that this will disappear after the function returns. Should I do this dynamically with 'new' or is this fine.

    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The Block will disappear at the end of the function, which means your pointer is pointing to nowhere in particular. new will help here, although you will have to make sure you delete things after they disappear.

  3. #3
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Y not make da array hold instances of th class insted of pointers?

  4. #4
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Thanks for the quick replies Tabstop and c++0x

    Interesting idea c++0x. Perhaps this would be OK as the class isn't that large. Just checking my understanding here - that the instance created within the function would be copied wholesale into the vector and although the destructor wil be called on the instance itself, there will still be a copy stored in the array? And as the vector uses the heap, there is no real saving to be had from using pointers?

    Perhaps this will be for practice then, but if I were to dynamically allocate instances of the Block - does this look OK for clearing it up? I haven't fully understood iterators yet, so I'm using indexing at the moment.

    Code:
    void BlockGenerator::reset() {
        for (int i = 0; i < BlockGenerator::blockList.size(); i++) {
            delete BlockGenerator::blockList[i];
            BlockGenerator::blockList[i] = NULL;
        }
        BlockGenerator::blockList.clear();
    }

  5. #5
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    My idea may not work if the class is polymorphic, which it may be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. quick question
    By ibleedart in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2007, 12:35 PM
  3. Replies: 2
    Last Post: 06-11-2006, 05:56 PM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  5. Quick Class Question (say that 3 times)
    By GrNxxDaY in forum C++ Programming
    Replies: 2
    Last Post: 07-25-2002, 11:16 AM