虚拟课堂有什么好处?

发布于 2024-09-16 11:12:41 字数 442 浏览 4 评论 0原文

可能的重复:
C++ 虚拟/纯虚拟解释

例如,我有:

class A {
    private: 
        int i; 
        int j; 

    public: 
        void k (int l, int m) { i=l; j=m; } 
        virtual int n (void); 
};

class B : public A { 
    public: 
        int n (void);
};

这个虚拟有什么好处?

Possible Duplicate:
C++ Virtual/Pure Virtual Explained

For example i have:

class A {
    private: 
        int i; 
        int j; 

    public: 
        void k (int l, int m) { i=l; j=m; } 
        virtual int n (void); 
};

class B : public A { 
    public: 
        int n (void);
};

What good is this virtual ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖兔^ω^ 2024-09-23 11:12:41

我喜欢用国际象棋游戏作为例子:

class ChessPiece
{
    public:
       virtual int Move() = 0;
};
class Queen: public ChessPiece
{
    public:
       virtual int Move() { /* STUFF */ }
};

bool AICode()
{
    int bestMove = 0;
    foreach(ChessPiece* loop = board.Pieces().begin(); loop != board.Pieces().end(); ++loop)
    {
        bestMove = max(bestMove, loop->Move());
    }
 }

AICode() 不需要知道它正在看哪一个棋子。
它所做的只是要求棋子看看它的最佳着法是什么。虚拟调度机制将计算出棋子的类型并调用正确的 Move() 方法。

I like to use the Chess Game as an example for this:

class ChessPiece
{
    public:
       virtual int Move() = 0;
};
class Queen: public ChessPiece
{
    public:
       virtual int Move() { /* STUFF */ }
};

bool AICode()
{
    int bestMove = 0;
    foreach(ChessPiece* loop = board.Pieces().begin(); loop != board.Pieces().end(); ++loop)
    {
        bestMove = max(bestMove, loop->Move());
    }
 }

The AICode() does not need to know which piece it is looking at.
All it does is ask the piece to see what its best move is. The virtual dispatch mechanism will work out the type of piece and call the correct Move() method.

千柳 2024-09-23 11:12:41

它是为了引发多态行为,这意味着客户端可以使用单个合约来调用不同的行为,而客户端不必了解每种可能的行为。

A* a = new B();
a->n(); // Will call B::n();

作为一个更具体的例子,这里有一些相当刻板的代码:(

struct Shape {
    GLfloat transform_[4][4];

    void render() const {
        glPushMatrix();
        glMultMatrixf(&transform_[0][0]);
        draw();
        glPopMatrix();
    }

    virtual void draw() const = 0;
};

struct Line : public Shape {
    GLfloat x1, y1, x2, y2;

    void draw() {
        glBegin();
        glVertex2f(x1, y1);
        glVertex2f(x2, y2);
        glEnd();
    }
};

struct Square : public Shape {
    GLfloat size;

    void draw() {
        GLfloat f = size/2;
        glBegin();
        glVertex2f(-f, -f);
        glVertex2f( f, -f);
        glVertex2f( f,  f);
        glVertex2f(-f,  f);
        glVertex2f(-f, -f);
        glEnd();
    }
};

void renderShapes(Shape* shapes, int nShapes) {
    for (int i = 0; i < nShapes; ++i) {
        shapes[i]->render();
    }
}

免责声明:上面的代码既不正确也不完整,并且当然不能作为良好图形代码的示例。它只是为了说明虚函数何时有用。

It's to induce polymorphic behaviour, which means that a single contract can be used by a client to invoke different behaviours, without the client having to know about every possible behaviour.

A* a = new B();
a->n(); // Will call B::n();

As a more concrete example, here some fairly stereotypical code:

struct Shape {
    GLfloat transform_[4][4];

    void render() const {
        glPushMatrix();
        glMultMatrixf(&transform_[0][0]);
        draw();
        glPopMatrix();
    }

    virtual void draw() const = 0;
};

struct Line : public Shape {
    GLfloat x1, y1, x2, y2;

    void draw() {
        glBegin();
        glVertex2f(x1, y1);
        glVertex2f(x2, y2);
        glEnd();
    }
};

struct Square : public Shape {
    GLfloat size;

    void draw() {
        GLfloat f = size/2;
        glBegin();
        glVertex2f(-f, -f);
        glVertex2f( f, -f);
        glVertex2f( f,  f);
        glVertex2f(-f,  f);
        glVertex2f(-f, -f);
        glEnd();
    }
};

void renderShapes(Shape* shapes, int nShapes) {
    for (int i = 0; i < nShapes; ++i) {
        shapes[i]->render();
    }
}

(Disclaimer: The above code is neither correct nor complete, and is certainly not to be held up as an example of good graphics code. It's just to illustrate when virtual functions are useful.

清旖 2024-09-23 11:12:41

我建议您参考这个关于多态性的优秀SO页面。

I refer you to this excellent SO page about polymorphism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文