Hi,

Is there any way to "protect" class constructors? So that a function which has a pointer to a class object can't just run the constructor again to mess up the class internals?

I had the idea of doing something like:

Code:
class TClass {
private:
int CreatedHash;
...
public:
TClass();
...
};

TClass::TClass()
{
 if (CreatedHash == 93245) return;
 ...
 CreatedHash = 93245;
}
This kind of thing ensures that a constructor can only be run once? Well, sort of. The value of "CreatedHash" in the above example is undefined at the time of the if statement. So it is technically possible for the value to be exactly 93245, and so the program would probably crash. So in all, while this is a possible method of protecting constructors, it is not a very good one.

Surely there is a better way?