The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Lecture/Lab #21
shared_ptr, pointers and run-time polymorphism
What will be the output of the following program?
struct car
{
car( std::string const& make_and_model )
: make_and_model( make_and_model ) {}
~car() {
std::cout << "Destroying " << make_and_model << std::endl;
}
std::string const make_and_model;
};
int main() {
std::vector<std::unique_ptr<car>> cars;
cars.push_back( std::make_unique<car>( "Tesla Model X" ) );
cars.push_back( std::make_unique<car>( "Honda Accord" ) );
for ( auto const& c : cars )
std::cout << c->make_and_model << std::endl;
cars[0] = std::make_unique<car>( "Hyundai Sonata" );
for ( auto const& c : cars )
std::cout << c->make_and_model << std::endl;
}
Shared object ownership
auto w = std::make_shared<widget>();
std::cout << "use_count: " << w.use_count() << std::endl;
if ( true ) {
auto w2 = w;
std::cout << "use_count: " << w.use_count() << std::endl;
std::cout << "use_count: " << w2.use_count() << std::endl;
}
std::cout << "use_count: " << w.use_count() << std::endl;
T* ptr
[control block]*
T object
reference count
weak count
other data (custom deleter, etc.)
Control block
Primary characteristics:
shared_ptr<T>
Heap
Use smart pointers to simplify memory management in the following program
Open the exercise template
Write your code, press Run to test
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
Click on the corresponding option in the "Lab21 exercises" poll in #general
struct car
{
virtual void print( std::ostream& out ) const
{ out << make_and_model; }
std::string const make_and_model;
};
struct truck : car { // truck is-a car
void print( std::ostream& out ) const override {
car::print( out );
out << ", capacity: " << capacity;
}
double capacity;
};
std::vector<car> cars;
cars.push_back( car( "Tesla Model X" ) );
cars.push_back( car( "Honda Accord" ) );
cars.push_back( truck( "Toyota Tacoma", 1620 ) );
for ( auto const& c : cars ) {
c.print( std::cout );
std::cout << std::endl;
}
Will this work?