반응형

Feedback : smile.k06@gmail.com


갑자기 새벽에 자다 깨서 그냥 shared_ptr 궁금한 점을 테스트 해봄.

궁금증은 이러하다.

  • shared_ptr로 new를 할 수 있다. reference count 로 포인터 연결 갯수를 알 수 있다.

  • 기본 포인터로 new를 할 수 있다. 몇개의 포인터가 연결이 되었나 알 수 없다.

    • 그렇다면 기본 포인터로 new를 한 녀석을 shared_ptr이 받을 수는 없을까?

 

였다.

 

1. 무식하게 그냥 넣어본다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <memory>
 
 
int main()
{
    // 일반 포인터로 Heap에 Instance 생성
    int* p = new int;
    
    // shared_ptr int타입으로 생성
    std::shared_ptr<int> shp;
    //shp = p;        // 불가
    
 
    //shared_ptr int타입으로 Heap에 Instance 생성
    std::shared_ptr<int> shp1(new int(3));
    shp = shp1;        // 가능
    shp1.reset();
 
    return 0;
}

→  12번째 줄처럼 불가.

당연히 shared_ptr은 class니까 하나 겠지만 서도.. -_-a

 

2. shared_ptr 내부를 뒤져봄

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
template<class _Ty>
    class _Ptr_base
    {    // base class for shared_ptr and weak_ptr
 
/// 중간생략
 
private:
    _Ty *_Ptr;
    _Ref_count_base *_Rep;
    template<class _Ty0>
        friend class _Ptr_base;
    };
 

→ 저기저 _Ptr이 private 이므로 접근이 불가 했음 ;;;

 

결론은 불가

 

 


 

 

반응형

+ Recent posts