@keskiverto
I'm new to programming so I'm not sure, can you tell me or direct me to some thread where I can learn how to check it. I'm trying to find answer for second day now.
#include <iostream>
class ConstructorDestructor {
public:
ConstructorDestructor(int);
~ConstructorDestructor();
private:
int id;
};
ConstructorDestructor::ConstructorDestructor(int number) {
id = number;
std::cout << "constructor" << id << std::endl;
}
ConstructorDestructor::~ConstructorDestructor() {
std::cout << "desctructor" << id << std::endl;
}
void something(void);
ConstructorDestructor first(1);
int main() {
something();
ConstructorDestructor second(2);
static ConstructorDestructor third(3);
return 0;
}
void something(void) {
ConstructorDestructor fourth(4);
}
this code for me print:
constructor1
constructor4
Edit:
If I define function 'something' before main than it call constructor for first, constructor and destructor for fourth, constructors for second and third and destructor for second.
Destructors for first and third are not printed.
This is what I get (for the second program) with the Visual Studio Debugger
constructor1
constructor4
desctructor4
constructor2
constructor3
desctructor2
desctructor3
desctructor1
C:\Users\xxx\source\repos\test\Debug\ConsoleApplication1.exe (process 11648) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->
Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
To verify, you can place a break point in the destructor (on line 17) and then run the program under the debugger.
(Do not issue a 'Stop Debugging' command; 'Continue' till the program exits.)
I suspect that your console window is closing B4 you can read the last 3 lines.
Try this:
From the Menu tabs choose "Tools" then "Options".
In the left side of the pop up window choose "Debugger" then "General".
In the right window the last line should say "Automatically close the console when debugging stops".
If the box is checked, uncheck it.
Do not forget to undo the change if you want the console window to close.
This is for VS 2017, but I believe that VS 2019 is the same.
It finally work.
For some reason I thinked that when I put breakpoint on the end of file I need only once to start debuging but in these case it stop after first two constructor are created and than I must press continue and it print the rest.