Question: I have a class 'Light' which functions out of the box, but I know that OpenGL only allows a finite number of lights to be active at any one time. According to `glGetIntegerfv(GL_MAX_LIGHTS, ..)` the max lights is 8, so I should only allow the Light class be instantiated up to 8 times whether in compile time or run.

I have tried a method of trying to solve the problem:

```
int Light::sCounter = 0;

Light::Light() : // initialiser-list
{
    sCounter++;

    if (sCounter > getMaxLightCount())
        // do something, but what?
}

```

Where sCounter is a `static int` and `getMaxLightCount()` calls the foresaid OpenGL function. If the counter is above the maximum number of lights, then something should happen; but I don't how I should respond. I can't return NULL or something in a constructor, but I have been looking at exceptions to solve the problem; though I generally dislike exceptions since it over-complicates the situation. I have also looked at implementing a Manager class, but that would make things counter-intuitive since it effectively means a user wouldn't be calling the Light class, but the Manager thereof.

What options do I have in limiting the number of instantiations of the Light class, and which solution would be effective in being intuitive for reuse?

Answer: 
If it's not too restrictive you could prevent instantiations of the `Light` class except when using a *factory*. The following example requires C++11 but could be made to work without it (albeit with more work, using boost, etc).

Even if the following example isn't sufficient perhaps it gives you some ideas.

Example Code
------------

```
#include <iostream>
#include <functional>
#include <memory>

class Light
{
public:
    using Ptr = std::unique_ptr<Light, std::function<void (Light*)>>;

    static void destroyLight(Ptr light)
    {
        // Do nothing, 'light' will be deleted when it falls out of scope
    }

    static Ptr createLight()
    {
        if (sInstanceCount < MAX_INSTANCE_COUNT)
        {
            ++sInstanceCount;
            return Ptr(new Light(), &Light::destroyLightImpl );
        }
        else
        {
            return Ptr();
        }
    }

private:
    Light() {} // Prevent others from creating instances
    ~Light() {} // Prevent others from deleting instances

    static void destroyLightImpl(Light* light)
    {
        delete light;
        --sInstanceCount;
    }

    static const int MAX_INSTANCE_COUNT = 3;
    static int sInstanceCount;
};

int Light::sInstanceCount = 0;

int main()
{
    Light::Ptr light1 = Light::createLight();
    Light::Ptr light2 = Light::createLight();
    std::cout << light1.get() << "\n";
    std::cout << light2.get() << "\n";

    Light::Ptr light3 = Light::createLight();
    Light::Ptr light4 = Light::createLight();
    std::cout << light3.get() << "\n";
    std::cout << light4.get() << "\n";

    Light::destroyLight(std::move(light3));
    Light::Ptr light5 = Light::createLight();
    std::cout << light5.get() << "\n";

    return 0;
}

```

Example Output
--------------

You can see here the first 3 instantiations succeed but the 4th doesn't. However, the 5th does work because the 3rd light has been deleted.

```
005872C0
005872F0
00587320
00000000
00587320

```
I think it would make more sense to have whatever class is creating the light maintain the count rather than trying to handle it within the `Light` class itself.

If you have many light-owners it would probably be best to set up some sort of pool from which lights can be obtained, and if desired you could even create a mechanism by which one client could request different clients to relinquish lights.