Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Order of constructor/destructor calls in c++

I have a PhoneBook class and a Contact class. My PhoneBook constructor should instantiate an array of 8 contacts. It actually works but when i print the constructors/destructors calls, I dont understand the output.

Here is my main followed by the classes with their respective constructors/destructors:

int main(void)
{
    PhoneBook   phonebook;

    //..

    return (0);
}
class Contact
{
    public:
        Contact();                  // construct a Contact
        ~Contact();                 // destruct a Contact
        void    init(void);         // init a Contact
        void    preview(int) const; // preview a Contact
        void    print(int) const;   // print a Contact
        void    setIndex(int);      // set index

    private:
        std::string _first_name;
        std::string _last_name;
        std::string _nickname;
        std::string _phone_number;
        std::string _darkest_secret;
        int         _index;
        std::string _printLen(std::string str) const;
        std::string _getInput(std::string str) const;
};

Contact::Contact()
{
    std::cout << "Contact created" << std::endl << std::flush;
}

Contact::~Contact()
{
    std::cout << "Contact destroyed" << std::endl << std::flush;
}
class PhoneBook
{
    public:
        PhoneBook(); // construct a PhoneBook
        ~PhoneBook(); // destroy a PhoneBook
        std::string welcome(void) const;
        void        addEntry(void);     // add a new contact
        void        previewContacts(void) const;
        void        searchEntry(void) const;

    private:
        Contact _contacts[8];   // pointer to the elements
        int     _size;          // number of contacts
};

PhoneBook::PhoneBook(): _size(0)
{
    std::cout << "PhoneBook created" << std::endl << std::flush;
    for (int i = 0; i < 8; i++)
        _contacts[i] = Contact();
}

PhoneBook::~PhoneBook()
{
    std::cout << "PhoneBook destroyed" << std::endl << std::flush;
}

Output I got:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Contact created
Contact created
Contact created
Contact created
Contact created
Contact created
Contact created
Contact created
PhoneBook created
Contact created
Contact destroyed
Contact created
Contact destroyed
Contact created
Contact destroyed
Contact created
Contact destroyed
Contact created
Contact destroyed
Contact created
Contact destroyed
Contact created
Contact destroyed
Contact created
Contact destroyed

Output I expected:

PhoneBook created
Contact created
Contact created
Contact created
Contact created
Contact created
Contact created
Contact created
Contact created
Contact destroyed
Contact destroyed
Contact destroyed
Contact destroyed
Contact destroyed
Contact destroyed
Contact destroyed
Contact destroyed
PhoneBook destroyed

I noted that if I declared my _contacts object as Contact *_contactsin my PhoneBook class, and I allocated it with new in my PhoneBook constructor, and deleted it with delete[] in my destructor, everything goes well, the expected output is printed.
Can someone please explain ?

>Solution :

_contacts gets initialized before you ever enter the body of the constructor.

PhoneBook::PhoneBook(): _size(0)
{
    std::cout << "PhoneBook created" << std::endl << std::flush;
    for (int i = 0; i < 8; i++)
        _contacts[i] = Contact();
}

Is actually

PhoneBook::PhoneBook(): _size(0), _contacts{}
{
    std::cout << "PhoneBook created" << std::endl << std::flush;
    for (int i = 0; i < 8; i++)
        _contacts[i] = Contact();
}

and it is that _contacts{} that causes the first eight constructor calls you see. The rest is from

for (int i = 0; i < 8; i++)
    _contacts[i] = Contact();

and you see a construction and destruction for each Contact() temporary object you create in the for loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading