Boost Shared Pointer Example

From Stack Overflow
Jump to: navigation, search

class SomeClass 
{
public:
	SomeClass();
	SomeClass(const char *ptr);
	~SomeClass();
	char *somePointer;
};

SomeClass::SomeClass() { this->somePointer = NULL }
SomeClass::SomeClass(char *ptr) { this->somePointer = strdup(ptr); }
SomeClass::~SomeClass()
{
	if (somePointer)
		free(somePointer)
	somePointer = NULL;
}

typedef std::vector<boost::shared_ptr<SomeClass> > SomeClassVector_t;

void MyFunction()
{
	const char* array[] = {"one", "two", "three"};
	SomeClassVector_t someClassVector;
	for (int i = 0; i < 3; i++)
	{
		boost::shared_ptr<SomeClass> theInstance(new SomeClass(array[i]));
		someClassVector.push_back(theInstance);
	}
}

Personal tools