1

我已经查阅了关于类和构造函数的各种指南和教程,但到目前为止,如何将两者结合到我的程序中对我来说还没有意义。我觉得一些巨大的逻辑块正在逃避我。如果有人能用人类语言解释构造函数应该如何为我的函数填充变量,我将非常感激。不仅仅是如何让它做我想让它做的事情,还有为什么它对程序有意义?我今年开始学习。谢谢你。这是要在 GBA 模拟器上编译的代码,尽管我遇到的问题纯粹是从 C++ 的角度来看的。我在程序中以粗体概述了这篇文章的附加评论。到目前为止,我如何理解我正在尝试做的是:

我创建了一个构造函数,该构造函数随后在程序的主循环中获取其中函数的变量值,在该程序中我第一次初始化类对象,然后在我为我简单调用的移动重绘框的部分在应该从构造函数存储其初始值的类对象上。

#include <stdint.h>
#include <stdlib.h>
#include "gba.h"

// A class with variables.
class CHARBOX
{
    public:
    int init_;
    int str_;
    int health_;
    int posx_;
    int posy_;
    int width_;
    int height_;
    int colour_; // someone advised me to name my class variables 
                 // with a special symbol attached.

    public:
// This is probably the part where I have not done things right. When this is 
// compiling, there is an error saying that there is no matching function to 
// call CHARBOX. Which I don`t understand completely.
        // Constructor.
    CHARBOX(int posx, int posy, int width, int height, int colour)
    {
        DrawBox(posx, posy, width, height, colour);
    }

    // Drawing functions.
    void DrawBox(int posx_, int posy_, int width_, int height_, int colour_)
    {
        for (int x = posx_; x < posx_ + width_; x++)
        {
            for (int y = posy_; y < posy_ + height_; y++)
            {
                PlotPixel8(x, y, colour_);
            }
        }
    }
};

// The entry point.
int main()
{
    // Put the display into bitmap mode 4, and enable background 2.
    REG_DISPCNT = MODE4 | BG2_ENABLE;

    // Defining some colour palettes.
    SetPaletteBG(1, RGB(90,0,0));
    SetPaletteBG(2, RGB(0,90,0));
    SetPaletteBG(3, RGB(0,0,90));
    SetPaletteBG(4, RGB(90,90,0));
    SetPaletteBG(5, RGB(90,0,90));

//Here is where the objects get initialized and the constructor is called.
        // Draw the player at a starting location.
    CHARBOX player(10, 24, 6, 8, 1);

    // Draw the enemy at a starting location.
    CHARBOX enemy(80, 24, 6, 8, 2);

// main loop.
    while (true);
    {
        // Clear screen and paint background.
        ClearScreen8(1);

        // Flip buffers to smoothen the drawing.
        void FlipBuffers();

        // Redraw the player.
        if ((REG_KEYINPUT & KEY_LEFT) == 0)
        {
            player.DrawBox(); // This is where the object gets called 
                                      // again to be redrawn.
            posx_--;
        }

        if ((REG_KEYINPUT & KEY_RIGHT) == 0)
        {
            player.DrawBox();
            posx_++;
        }

        if ((REG_KEYINPUT & KEY_UP) == 0)
        {
            player.DrawBox();
            posy_--;
        }

        if ((REG_KEYINPUT & KEY_DOWN) == 0)
        {
            player.DrawBox();
            posy_++;
        }
    WaitVSync();
    }
return 0;
}
4

1 回答 1

1

您需要使用成员初始化列表来初始化您的类成员:

CHARBOX(int posx, int posy, int width, int height, int colour):posx_(posx),posy_(posy),width_(width),height_(height), colour_(colour)
{

}

好读:
构造函数中这个奇怪的冒号成员(“:”)语法是什么?

有人建议我用附加的特殊符号命名我的类变量。

这样您就可以区分成员变量名称和传递的函数参数名称。您可以简单地选择不同的名称,这不是必需的,它应该没问题。

为什么它对程序有意义?

C++ 中的构造函数是一个特殊的成员函数,每当创建类的对象时都会调用它。构造函数的目的是提供正确初始化对象成员的机会。例如:在您的情况下,width_、height_ 等。

一旦构造了对象,就假定类成员处于有效和确定的状态,以便程序可以使用它们。在您的情况下,除非您在构造函数中初始化成员,否则它们将具有不确定的值,即:任何随机值。您真的不希望成员函数获取width_并返回垃圾值。所以你需要初始化它们。

于 2013-01-26T05:38:07.723 回答