Skip to content

Instantly share code, notes, and snippets.

@StarJade-Park
Created October 16, 2019 15:26
Show Gist options
  • Save StarJade-Park/b4e64a22d13ffe8adb287df96c2fb678 to your computer and use it in GitHub Desktop.
Save StarJade-Park/b4e64a22d13ffe8adb287df96c2fb678 to your computer and use it in GitHub Desktop.
#include "pch.h"
#include <iostream>
#include <memory>
using namespace std;
template<typename Object>
void Deleter(Object* obj)
{
delete obj;
};
class A
{
friend int main(void);
template<typename Object>
friend void Deleter(Object* obj);
public:
int intA = 0;
protected:
A( ) = default;
virtual ~A( ) = default;
};
class B : public A
{
friend int main(void);
template<typename Object>
friend void Deleter(Object* obj);
public:
int intB = 10;
protected:
B( ) = default;
~B( ) = default;
};
int main()
{
shared_ptr<B> b = shared_ptr<B>(new B, Deleter<B>);
shared_ptr<A> a = shared_ptr<A>(new A, Deleter<A>);
a->intA = 9999;
cout << "a : " << a->intA << endl;
cout << "a address: " << a.get( ) << endl;
b->intA = -9999;
b->intA = 1234;
cout << "b : " << b->intA << ", " << b->intB << endl;
cout << "b address: " << b.get( ) << endl << endl;
cout << "dynamic_pointer_cast" << endl;
a = dynamic_pointer_cast<A>(b);
cout << "a : " << a->intA << endl;
cout << "a address: " << a.get( ) << endl;
return 0;
}
@StarJade-Park
Copy link
Author

Result

a : 9999
a address: 00ACDC70
b : 1234, 10
b address: 00ACD880

dynamic_pointer_cast
a : 1234
a address: 00ACD880

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment