I was practicing working with classes and the static property included with them and came up with a simple ID counter and max number list. I also added a quick debug error throw if it goes over. Here's what I came up with:

#include <iostream>

// A class that has static variables to save information for the ID's of its instances.
class HoldIdentity{
    static int const maxInstances = 20;
    static int created; // = 0; error
    static int getMax(){
        return maxInstances;
    // A static function that will return an array on the heap of every /possible/ remaining Identity Holder. 
    static HoldIdentity* const createAll(){
      int length = maxInstances - created;
      HoldIdentity*JAM = new HoldIdentity[maxInstances - created];
      for (int i = 0; created < maxInstances; JAM[i++]=HoldIdentity());
      return JAM;
    int id;
        id = created++;
        if (created > maxInstances){
            std::string error = "To many instances!";
            throw (error);
    // Destructor displays the id. 
    void displayID() const{
        std::cout << "my id:" << id << std::endl;

//int HoldIdentity::created = 0;
int main(){
    using namespace std;
    HoldIdentity numba1{};
    HoldIdentity *allInstances = HoldIdentity::createAll();

    // because creating more than the max causes an error.
    // I suppose I should have put all prior code in here as well. 
        HoldIdentity numbaPast{};
        HoldIdentity numbaPast2{};
        HoldIdentity numbaPast3{};
    catch (...){ 
        // I think I could have used a string to retrieve the message?
        cout << " too many instances! ";


    // free array from the heap. 
    delete[] allInstances;
    return 0;

edit: Is there any little bit of code here that could be improved? I've only just started working on C++(again) this week so I'm sure there's many alternatives. But specifically if my own code for determining and handling IDS are bad. Besides that, is my error handling a good practice?

  • 2
    \$\begingroup\$ Once you start looking into templates, you can try this: en.wikipedia.org/wiki/… \$\endgroup\$ – glampert Jul 24 '15 at 1:39
  • \$\begingroup\$ @Barry I didn't mean to be ambiguous here. Sorry guys, I updated the code to be more thorough (hopefully) \$\endgroup\$ – Andrew Jul 24 '15 at 14:59
  • \$\begingroup\$ @Barry It's supposed to be extremely minimal. It's only supposed to have an ID. That's its only purpose and I stated earlier that it was just practice with the Static member. I just was curious if my code doing this was good, or a bad practice. I'm sure maybe in the future I could make a protected return id to a friend class to make it more useful, but it was just to practice static... \$\endgroup\$ – Andrew Jul 24 '15 at 15:17

Why don't you initialize created in the definition?

What does JAM stand for?

Why do you calculate length but never use it?

I'm not 100% sure on this one but I think you don't need the for loop calling the constructor. new should construct every object in the array separately.

  • \$\begingroup\$ JAM was just a quick name, I changed it now that you mentioned it. The new operator did in fact instantiate classes and the for-loop was not needed, that's amazing to me haha. With your suggestions I ended up with: return new SpecialClass[maxInstances - created]; :). And "Why don't you initialize created in the definition?" -> IntelliSense: a member with an in-class initializer must be const I'm not using C++11. \$\endgroup\$ – Andrew Jul 24 '15 at 14:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.