Hi,
I am wondering why does the below code not behave as i would expect:
Code:
#include <iostream>
using namespace std;
struct error{};
class Trace{
public:
Trace(const char *string_pt = "done") ;
~Trace();
private:
const char *string;
};
// declare constructor
Trace::Trace (const char *string_pt)
: string(string_pt)
{
cout << "+ " << string << endl;
}
// declare destructor
Trace::~Trace(){
cout << "- " << string << endl;
}
int main(){
Trace string("A1");
Trace string1("A2");
for(int i = 0; i<3;i++){
Trace tp("inLoop");
}
Trace *tpp =0;
{
Trace string("B1");
Trace *string1 = new Trace("B2");
tpp = new Trace("Here I am!");
}
delete tpp;
return 0;
}
During destruction it destructs everything except string1. This i claim based upon the output:
Code:
+ A1
+ A2
+ inLoop
- inLoop
+ inLoop
- inLoop
+ inLoop
- inLoop
+ B1
+ B2
+ Here I am!
- B1
- Here I am!
- A2
- A1
What happened to B2 ???
Thank you
baxy