Creating Objects Dynamically

How would I implement a way that i could create as many objects as the user wants?

Let's say every time a user clicks space bar a new cat object would be created, because right now i create lots of objects without initializing and every time i get a input i assign values to the object, the downside is that is limited for the amount of objects i create.

As well i don't want to cause a memory leak, specially in c++ because i need to use the delete keyword.
Use std::vector<> http://www.mochima.com/tutorials/vectors.html

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>

struct cat
{
    enum colour_t { TABBY, HARLEQUIN, TORTOISESHELL, MALTESE };

    cat( const std::string& name, colour_t clr ) : name(name), colour(clr) {}

    std::string name ;
    colour_t colour ;
};

int main()
{
    // create four cats
    std::vector<cat> felines { { "Felix", cat::HARLEQUIN }, { "Sonja", cat::MALTESE },
                               { "Lilian", cat::TORTOISESHELL }, { "Baron", cat::TABBY } };

    // create another cat
    felines.emplace_back( "Mina", cat::HARLEQUIN ) ;

    // one more
    felines.emplace_back( "Orion", cat::TORTOISESHELL ) ;


    // print out the names
    for( const cat& c : felines ) std::cout << c.name << '\n' ;
}

http://coliru.stacked-crooked.com/a/451f3fa34f324461
Topic archived. No new replies allowed.