Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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{
private:
    static int const maxInstances = 20;
    static int created; // = 0; error
public:
    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;
    };
private:
    int id;
public:
    HoldIdentity(){
        id = created++;
        if (created > maxInstances){
            created--;
            std::string error = "To many instances!";
            throw (error);
        }
    }
    // Destructor displays the id. 
    ~HoldIdentity(){
        displayID();
    }
    void displayID() const{
        std::cout << "my id:" << id << std::endl;
    }
};


//int HoldIdentity::created = 0;
int main(){
    using namespace std;
    HoldIdentity numba1{};
    numba1.displayID();
    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. 
    try{
        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;
    system("pause");
    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?

share|improve this question

closed as too broad by Barry, jacwah, Ryan, Abbas, Ethan Bierlein Jul 24 at 15:16

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Once you start looking into templates, you can try this: en.wikipedia.org/wiki/… –  glampert Jul 24 at 1:39
    
I don't understand what this class is supposed to be used for. –  Barry Jul 24 at 12:58
    
@Barry I didn't mean to be ambiguous here. Sorry guys, I updated the code to be more thorough (hopefully) –  Lemony-Andrew Jul 24 at 14:59
    
@Lemony-Andrew I still don't understand what this class is supposed to be used for. Do you mean for it to be a base class adding IDs to other classes? As a standalone class, it's largely useless - you don't even have access to the ID, all you can do is print it... –  Barry Jul 24 at 15:02
    
@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... –  Lemony-Andrew Jul 24 at 15:17

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
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. –  Lemony-Andrew Jul 24 at 14:46

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