I tried this code on multiply online compilers and it work well but when I try it on my there are problems(well not exactly errors, but it not working as expected).
The destructor is not called for both static and gloabl objects. And if I put function create behind main then after it call constructor for objects in create, it does not doing anything afterwards, does not calling any destructor and does not even print message that are after function create.
I'm using Microsoft Visual Studio Community 2017 version 15.9.17 if it is of any use.
So is there any solution to these or I need to install Visual Studio all over again?
// Expected
Object 1 constructor runs (global before main)
MAIN FUNCTION: EXECUTION BEGINS
Object 2 constructor runs (local automatic in main)
Object 3 constructor runs (local static in main)
CREATE FUNCTIONS: EXECUTION BEGINS
Object 5 constructor runs (local automatic in create)
Object 6 constructor runs (local static in create)
Object 7 constructor runs (local automatic in create)
CREATE FUNCTION: EXECUTION ENDS
Object 7 destructor runs (local automatic in create)
Object 5 destructor runs (local automatic in create)
MAIN FUNCTION: EXECUTION RESUMES
Object 4 constructor runs (local automatic in main)
MAIN FUNCTION: EXECUTION ENDS
Object 4 destructor runs (local automatic in main)
Object 2 destructor runs (local automatic in main)
Object 6 destructor runs (local static in create)
Object 3 destructor runs (local static in main)
Object 1 destructor runs (global before main)
// Acctual
Object 1 constructor runs (global before main)
MAIN FUNCTION: EXECUTION BEGINS
Object 2 constructor runs (local automatic in main)
Object 3 constructor runs (local static in main)
CREATE FUNCTIONS: EXECUTION BEGINS
Object 5 constructor runs (local automatic in create)
Object 6 constructor runs (local static in create)
Object 7 constructor runs (local automatic in create)
CREATE FUNCTION: EXECUTION ENDS
Object 7 destructor runs (local automatic in create)
Object 5 destructor runs (local automatic in create)
MAIN FUNCTION: EXECUTION RESUMES
Object 4 constructor runs (local automatic in main)
MAIN FUNCTION: EXECUTION ENDS
Object 4 destructor runs (local automatic in main)
Object 2 destructor runs (local automatic in main)
Acctual if I put function create behind main
Object 1 constructor runs (global before main)
MAIN FUNCTION: EXECUTION BEGINS
Object 2 constructor runs (local automatic in main)
Object 3 constructor runs (local static in main)
CREATE FUNCTIONS: EXECUTION BEGINS
Object 5 constructor runs (local automatic in create)
Object 6 constructor runs (local static in create)
Object 7 constructor runs (local automatic in create)
CREATE FUNCTION: EXECUTION ENDS
#include <iostream>
using std::cout;
using std::endl;
class CreateAndDestroy {
public:
CreateAndDestroy(int, const char*); // constructor
~CreateAndDestroy(); // destructor
private:
int objectID;
const char* message;
}; // end class CreateAndDestroy
// constructor
CreateAndDestroy::CreateAndDestroy(int objectNumber, const char* messagePtr) {
objectID = objectNumber;
message = messagePtr;
cout << "Object " << objectID << " constructor runs " << message << endl;
} // end CreateAndDestroy constructor
// destructor
CreateAndDestroy::~CreateAndDestroy() {
cout << (objectID == 1 || objectID == 6 ? "\n" : "");
cout << "Object " << objectID << " destructor runs " << message << endl;
} // end ~CreateAndDestroy destructor
void create(void); // prototype
// functions to create objects
void create(void) {
cout << "\nCREATE FUNCTIONS: EXECUTION BEGINS" << endl;
CreateAndDestroy fifth(5, "(local automatic in create)");
static CreateAndDestroy sixth(6, "(local static in create)");
CreateAndDestroy seventh(7, "(local automatic in create)");
cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl;
} // end function create
// global object
CreateAndDestroy first(1, "(global before main)");
int main(int argc, char *argv) {
cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
CreateAndDestroy second(2, "(local automatic in main)");
static CreateAndDestroy third(3, "(local static in main)");
create(); // call function to create objects
cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl;
CreateAndDestroy fourth(4, "(local automatic in main)");
cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl;
return 0;
} // end main ```