Thursday, April 17, 2008

Quick std::vector question - GameDev.Net Discussion Forums

Quick std::vector question - GameDev.Net Discussion Forums: "boost::shared_ptr"
#include 
#include
#include

class bar
{
public:
bar() {std::cout << "bar constructor called" << std::endl;}
~bar() {std::cout << "bar destructor called" << std::endl;}
};


class foo
{
boost::shared_ptr i;

public:
foo();
void doSomethingWithTheBar();
};


foo::foo() : i(new bar)
{
std::cout << "newing a bar" << std::endl;
std::cout << "bar's address: " << i << std::endl;

}

void foo::doSomethingWithTheBar()
{
std::cout << "Do something with bar at address: " << i << std::endl;
}


int main()
{
std::vector vec;
{
foo f;
vec.push_back(f);
}
vec[0].doSomethingWithTheBar();
std::cout << "It's ok this time, as the memory at that address hasn't been deleted" << std::endl;
}



No comments:

Post a Comment