-4
Когда я запускаю следующий код, я вижу, что указатель sptr имеет значение NULL внутри деструктора B.C++ непредвиденное поведение деструктора
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
class B
{
public:
~B()
{
std::cout<< "B Dtor ";
// Access sptr
}
void RunB()
{
sptr.reset(new int(2));
}
private:
boost::shared_ptr<int> sptr;
};
class A
{
public:
void RunA()
{
b[1].RunB();
}
private:
B b[1];
};
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
A a;
a.RunA();
return 0;
}
Однако при изменении B B [1] B B, т.е. из массива на обычный объект, точка sptr не находится внутри B desctructor NULL. Зачем? Кто-то может помочь? Я совершенно не знаю.
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
class B
{
public:
~B()
{
std::cout<< "B Dtor ";
// Access sptr
}
void RunB()
{
sptr.reset(new int(2));
}
private:
boost::shared_ptr<int> sptr;
};
class A
{
public:
void RunA()
{
b.RunB();
}
private:
B b;
};
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
A a;
a.RunA();
return 0;
}