Почему счетчик shared_ptr<drived>
увеличивается, когда я передаю его функции, которая ожидает const shared_ptr<base>&
?
В этот вопрос один из ответов упоминает:
shared_ptr<Base> and shared_ptr<Derived> are not covariant
Я подозреваю, что это относится к моему вопросу. Что это значит, что они не являются ковариантными?
Вот фрагмент кода для демонстрации сценария:
#include <iostream>
#include <memory>
class Base {};
class Derived : public Base {};
void f(const std::shared_ptr<Base>& x)
{
std::cout << "in function expecting const shared_ptr<Base>& - Use count: " << x.use_count() << std::endl;
}
int main(int argc, char const *argv[])
{
std::cout << "Base class" << std::endl;
auto a = std::make_shared<Base>();
std::cout << "Created shared_ptr: Initial use count: " << a.use_count() << std::endl;
f(a);
std::cout << "------------------\nChild class" << std::endl;
auto b = std::make_shared<Derived>();
std::cout << "Created shared_ptr. Initial use count: " << b.use_count() << std::endl;
f(b);
return 0;
}
Результаты:
>> g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
Base class
Created shared_ptr: Initial use count: 1
in function expecting const shared_ptr<Base>& - Use count: 1
------------------
Child class
Created shared_ptr. Initial use count: 1
in function expecting const shared_ptr<Base>& - Use count: 2