C++ class-object?
In C++, How one can restrict a class so tht it can have oly one object?
|
You can't, really. But you can, effectively. The question you need to ask is "What do I want to see happen when the code attempts to create a second instance?".
Still, this situation does arise often enough and is usually handled through the use of static class variables. For more information do a google search on "c++ singleton pattern". |
What you want is known as a singleton and in a singleton class the constructor is private.
Instead you have a public static member function that returns a dynamically allocated instance of the class and this instance is a static data member of the class itself. Code:
class mysingleton |
All times are GMT -5. The time now is 04:49 PM. |