Let's say there's a class:
class X {
public:
static X& get_instance(int foo, int bar);
private:
X();
int foo, bar;
}
If I wrote this correctly then get_instance
is a named constructor. Now here's what I'm trying to do;
Let's create an instance of X;
X& first = X::get_instance(3, 5);
X& second = X::get_instance(4, 6);
X& third = X::get_instance(3, 5);
I want to write get_instance
in such a way that, when it is called with the same parameters twice, instead of creating a new instance, it should just return a reference to the old one. When the instances created with parameters (3, 5) all get deleted, then creating a new instance with (3, 5) create a new instance.
I imagined this would be possible by overloading the equality test operator and keeping track of every object the constructor has created so far. But this is C++, and there has to be a better way to do it.