Re: C++ Classes constructor
Quote:
Originally posted by ChimpFace9000
If i have a class name Class. The the function "Class" is called for every instance of the class when it starts, and "~Class" is class is called for every instance when it exits. My question is, is there a function that gets called only once when it starts, no matter how many instances of the class?
I ask this because im making a window into a class and in the constuctor i register the window class. I figure this might cause problems if theres more than one instance of the class. Or its just kind of a waste to register the window more than once.
The Singleton pattern might help you here. The idea is that you have a private constructor, and you request the object which is created statically:
Code:
class Singleton {
public:
inline Singleton& instance() {
static Singleton myself;
return myself;
}
private:
inline Singleton() { }
};
...is this the sort of thing you need?