2024/9/5

test gdb step into virtual function

#include <iostream>
#include <cstdlib>
#include <ctime>

class Base {
public:
    virtual void show() {
        std::cout << "Base class show function" << std::endl;
    }
    virtual ~Base() = default;
};

class Derived1 : public Base {
public:
    void show() override {
        std::cout << "Derived1 class show function" << std::endl;
    }
};

class Derived2 : public Base {
public:
    void show() override {
        std::cout << "Derived2 class show function" << std::endl;
    }
};

void callShow(Base* obj) {
    obj->show();
}

int main() {
    std::srand(std::time(0)); // 設定亂數種子
    Base* b;

    if (std::rand() % 2 == 0) {
        b = new Derived1();
    } else {
        b = new Derived2();
    }

    callShow(b);
    delete b;
    return 0;
}

沒有留言:

張貼留言