Skip to content

Instantly share code, notes, and snippets.

@remyroez
Created November 17, 2016 05:01
Show Gist options
  • Save remyroez/ad867810ef151ac6fce9b2d91925c109 to your computer and use it in GitHub Desktop.
Save remyroez/ad867810ef151ac6fce9b2d91925c109 to your computer and use it in GitHub Desktop.
基底クラスにおいてデストラクタが非仮想の場合、派生クラスのデストラクタが呼ばれる場合、呼ばれない場合
#include <iostream>
#include <memory>
class foo {
public:
foo() { std::cout << __func__ << std::endl; }
~foo() { std::cout << __func__ << std::endl; }
};
class bar : public foo {
public:
bar() { std::cout << __func__ << std::endl; }
~bar() { std::cout << __func__ << std::endl; }
};
int main()
{
{
foo *a = new bar();
delete a;
}
std::cout << "----------" << std::endl;
{
std::unique_ptr<foo> a = std::make_unique<bar>();
}
std::cout << "----------" << std::endl;
{
std::shared_ptr<foo> a = std::make_unique<bar>();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment