편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.12.28

    c++ 코드 에러 뜨는 이유 알려주세요


    #include<iostream>
    
    using namespace std;
    
    class First
    {
    public:
        void SimpleFunc() { cout << "First" << endl; };
    };
    
    class Second :public First
    {
    public:
        virtual void SimpleFunc() { cout << "Second" << endl; };
    
    };
    
    
    int main(void)
    {
        First* ptr = new First();
        ptr->SimpleFunc();
        delete ptr;
    
        ptr = new Second();
        ptr->SimpleFunc();
        delete ptr;
    
        return 0;
    }
    

    void SimpleFunc() { cout << "First" << endl; }; 이 부분을 virtual 로 하면 정상 작동하는데 안하면 에러 뜨네요.

    마지막 delete ptr에서 에러 뜨네요 이유가 뭐죠?

  • 프로필 지훈님의 편집
    날짜2020.12.28

    c++ 코드 에러 뜨는 이유 알려주세요


    include

    using namespace std;

    class First { public: void SimpleFunc() { cout << "First" << endl; }; };

    class Second :public First { public: virtual void SimpleFunc() { cout << "Second" << endl; };

    };

    int main(void) { First* ptr = new First(); ptr->SimpleFunc(); delete ptr;

    ptr = new Second();
    ptr->SimpleFunc();
    delete ptr;
    
    return 0;
    

    } void SimpleFunc() { cout << "First" << endl; }; 이 부분을 virtual 로 하면 정상 작동하는데 안하면 에러 뜨네요 마지막 delete ptr에서 에러 뜨네요 이유가 머죠?