In my studies, I have learned that if you want to prevent users from calling the default constructor of a class, you can make it private:
class Person
{
public:
Person(const Person&);
~Person();
private:
Person(); //Any call to this will cause a compiler error.
};
What I don’t understand is when I create an uninitialized array of the class, it gives me a syntax error saying that it is private:
Person * ptr; //Works just fine.
Person arr[1]; //Syntax error: 'Person::Person()' is private
This would leave me to believe that it makes an attempt to call the default constructor when the array is created. But this doesn’t make any sense to me since I’m not actually creating any real objects.
Here is your mistake. In your second example (the one that fails) you are creating a real object. You are defining an array of Persons (people?!) with one element/object.
Therefore you are creating an object to go into that array.
Remember when an object gets created – what happens?
A constructor is called, if there is no suitable definied constructor what does the compiler
do?
It uses the default one – but it cannot because it is private.