어제 만든 템플릿함수 클래스내부에 넣어서 구현


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
class Obj {
public:
    virtual void DoSomthing() {
        cout << "Obj호출" << endl;
    }
};
 
class A : public Obj {
public:
    void DoSomthing() override {
        cout << "A호출" << endl;
    }
};
 
class DataManage {
public:
    map<string, unique_ptr<Obj>(DataManage::*)()> FNptrMap;
    vector<unique_ptr<Obj>> dataVec;
 
public:
    template<typename T>
    unique_ptr<Obj> TestFN()
    {
        return make_unique<T>();
    }
    void push_back(string p_str) {
        dataVec.push_back((this->*FNptrMap[p_str])());
    }
    void Update() {
        dataVec[0]->DoSomthing();
    }
 
public:
    DataManage() {
        FNptrMap.emplace("A"&DataManage::TestFN<A>);
    }
};
 
int main()
{
    DataManage dataManager;
    dataManager.push_back("A");
    dataManager.Update();
}
cs



유니크 주소 값복사하기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class A {
public:
    int a;
    A(){}
    A(int p_1) { a = p_1; }
    void DoSomThing() {
        cout << a << " 실행" << endl;
    }
};
 
class Data
{
public:
    vector<unique_ptr<A>> aVec;
 
    void setVec(unique_ptr<A> p_1) {
        aVec.push_back(move(p_1));
    }
    unique_ptr<A> getVec(int p_1) {
        unique_ptr<A> temp = make_unique<A>();
        *temp = *aVec.at(p_1).get();
        return move(temp);
    }
    unique_ptr<A> Clone(unique_ptr<A> p_1) {
    }
};
 
void main() {
    Data data;
    unique_ptr<A> temp = make_unique<A>(5);
    //추가할땐이동하지만
    data.setVec(move(temp));
    //가져올땐 복사
    unique_ptr<A> i = data.getVec(0);
    
    ++i->a;
    i->DoSomThing();
    data.aVec[0]->DoSomThing();
}
cs



i는 6출력되고

배열에있는건 5나옴


씻고 책이나 읽어야겟다

코드에서 고급적인건 모르는티가 많이나는듯

기초적인것들로 어거지로 짜서 구현하고