Created
November 17, 2016 05:01
-
-
Save remyroez/ad867810ef151ac6fce9b2d91925c109 to your computer and use it in GitHub Desktop.
基底クラスにおいてデストラクタが非仮想の場合、派生クラスのデストラクタが呼ばれる場合、呼ばれない場合
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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