Skip to content

Instantly share code, notes, and snippets.

@kioba
Last active November 8, 2015 23:06
Show Gist options
  • Save kioba/28edb80659c3571808ec to your computer and use it in GitHub Desktop.
Save kioba/28edb80659c3571808ec to your computer and use it in GitHub Desktop.
std::shared_ptr question! "Is it correct?"
#include <iostream>
#include <memory>
class Base
{
public:
Base()
{
std::cout << "Base Constructor!" << std::endl;
}
~Base()
{
std::cout << "Base Destructor!" << std::endl;
}
};
class Derived :
public Base
{
public:
Derived()
{
std::cout << "Derived Constructor!" << std::endl;
}
~Derived()
{
std::cout << "Derived Destructor!" << std::endl;
}
void print()
{
std::cout << "Derived Print!" << std::endl;
}
};
int main()
{
std::shared_ptr<Base> derived(new Derived);
std::shared_ptr<Derived> casted(static_cast<Derived*>(derived.get()));
casted->print();
casted.reset();
std::cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment