According to my understanding, when a const shared_ptr& is upcasted, it creates a new shared pointer of the based class pointing to the same (casted) object.
#include <iostream>
#include <memory>
class Animal
{
};
class Cat : public Animal
{
};
void display(const std::shared_ptr<Animal>& animal)
{
std::cout << animal.use_count() << std::endl;
}
int main()
{
auto cat = std::make_shared<Cat>();
std::cout << cat.use_count() << std::endl;
display(cat);
std::cout << cat.use_count() << std::endl;
}
The output of the above code is as follow.
sujith@AKLJoincggDLEd:~/sujith$ g++ -std=c++17 main.cpp
sujith@AKLJoincggDLEd:~/sujith$ ./a.out
1
2
1
sujith@AKLJoincggDLEd:~/sujith$
My question is what operator does this? Can we get the same behaviour for other reference upcasting as well? Or is it speficially handled for shared_ptr references?
Thank you for looking into this.