Thanks for all the replies to my last question! Another problem, though:
Okay, this doesn't make ANY sense at all. All I'm doing is instantiating a TestClass. Its constructor takes a string. It has a destructor. I've set it up so it prints when the constructor or destructor is called. Normally,
the destructor is called twice. When I change it so that the constructor takes an int,
the destructor is called only once. What the hell.
#include <iostream>
#include <string>
using namespace std;
struct TestClass{
TestClass(string a){
cout << "Constructor called" << endl;
}
~TestClass(){
cout << "Destructor called" << endl;
}
};
void Test(){
TestClass tc = TestClass("test");
}
int main(){
Test();
// breakpoint here
}
#include <iostream>
#include <string>
using namespace std;
struct TestClass{
TestClass(int a){
cout << "Constructor called" << endl;
}
~TestClass(){
cout << "Destructor called" << endl;
}
};
void Test(){
TestClass tc = TestClass(42);
}
int main(){
Test();
// breakpoint here
}
The only difference between the two programs is what parameter the TestClass constructor takes. Program 1 takes a string, Program 2 takes an int. Here are their outputs:
Constructor called
Destructor called
Destructor called
Constructor called
Destructor called
POSTSCRIPT: I just found out if I changed this line in Program 1:
TestClass tc = TestClass("test");
to:
TestClass tc("test");
then it works as it should, with only one destructor call. But still... what is going on exactly?