我正在研究一个c ++项目,我无法通过引用传递rect0对象

时间:2015-02-22 06:52:18

标签: c++ pass-by-reference

#define NOMINMAX // prevent Windows API from conflicting with "min" and "max"

#include <stdio.h>   // C-style output. printf(char*,...), putchar(int)
#include <windows.h> // SetConsoleCursorPosition(HANDLE,COORD)
#include <conio.h>   // _getch()

/**
* moves the console cursor to the given x/y coordinate
* 0, 0 is the upper-left hand coordinate. Standard consoles are 80x24.
* @param x
* @param y
*/
void moveCursor(int x, int y)
{
    COORD c = {x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

struct Vec2
{
    short x, y;
    Vec2() : x(0), y(0) { }
    Vec2(int x, int y) : x(x), y(y) { }
    void add(Vec2 v)
    {
        x += v.x;
        y += v.y;
    }
    void operator+=(const Vec2 other_)
    {
        x += other_.x;
        y += other_.y;
    };
};

class Rect
{
    Vec2 min, max;
public:
    Rect(int minx, int miny, int maxx, int maxy)
    :min(minx,miny),max(maxx,maxy)
    {}
    Rect(){}
    void draw(const char letter) const
    {
        for(int row = min.y; row < max.y; row++)
        {
            for(int col = min.x; col < max.x; col++)
            {
                if(row >= 0 && col >= 0)
                {
                    moveCursor(col, row);
                    putchar(letter);
                }
            }
        }
    }
    bool isOverlapping(Rect const & r) const
    {
        return !( min.x >= r.max.x || max.x <= r.min.x
           || min.y >= r.max.y || max.y <= r.min.y);
    }
    void translate(Vec2 const & delta)
    {
        min += (delta);
        max += (delta);
    }
    void setMin(Vec2 const & min)
    {
        this->min = min;
    }
    void setMax(Vec2 const & max)
    {
        this->max = max;
    }
    Vec2 getMin()
    {
        return min;
    }
    Vec2 getMax()
    {
        return max;
    }
    void setRandom(Rect &r)
    {
        int posX, posY, height, width;
        posX = rand() % 51;
        posY = rand() % 21;
        height = 2 + rand() % 11;
        width = 2 + rand() % 11;

        height = height / 2;
        width = width / 2;

        min.x = posX - width;
        min.y = posY - height;
        max.x = posX + width;
        max.y = posY + height;
    }

};


int main()
{
    // initialization
    Rect * userRect = new Rect(7, 5, 10, 9);
    Rect rect0(10, 2, 14, 4);
    Rect rect1(1, 6, 5, 15);
    Rect testSetRandom;

    int userInput;
    do
    {
        // draw
        rect0.draw('0');
        rect1.draw('1');
        moveCursor(0, 0);   // re-print instructions
        printf("move with 'w', 'a', 's', and 'd'");
        userRect->draw('#');
        // user input
        userInput = _getch();
        // update
        Vec2 move;
        switch(userInput)
        {
        case 'w':   move = Vec2( 0,-1); break;
        case 'a':   move = Vec2(-1, 0); break;
        case 's':   move = Vec2( 0,+1); break;
        case 'd':   move = Vec2(+1, 0); break;
        }
       userRect->draw(' '); // un-draw before moving
       userRect->translate(move);
    }while(userInput != 27); // escape key
    delete userRect;
    return 0;
}

//   Here is what I am trying to do:
//   3) Random rectangles, by reference and by pointer
//   a) create a method with the method signature "void setRandom(Rect & r)".
//      This function will give the passed-in Rect object a random location.
//      The random x should be between 0 and 50 x. The random y should be
//      between 0 and 20. Limit the possible width and height to a minimum of 2
//      and a maximum of 10.
//   b) test "void setRandom(Rect & r)" on the local Rect object "rect0".
//   c) create a method with the method signature
//      "void setRandomByPointer(Rect * r)", which functions the same as
//      "void setRandom(Rect & r)", except that the argument is
//      passed-by-pointer.
//   d) test "void setRandomByPointer(Rect * r)" on the local Rect object
//      "rect1".

上面的评论是对我要做的事情的解释。我觉得我过于复杂一件非常简单的事情。我想创建一个方法,通过引用获取对象并在随机位置绘制它。然后我想通过指针做同样的事情。我开始的两个签名是“void setRandom(Rect&amp; r)”和“void setRandomByPointer(Rect * r)”。我将使用对象rect0(10,2,14,4)测试它们中的每一个。

1 个答案:

答案 0 :(得分:0)

void setRandom(Rect& r)
{
    int posX, posY, height, width;
    posX = rand() % 51;
    posY = rand() % 21;
    height = 2 + rand() % 11;
    width = 2 + rand() % 11;

    height = height / 2;
    width = width / 2;

    r.min.x = posX - width;
    r.min.y = posY - height;
    r.max.x = posX + width;
    r.max.y = posY + height;
}

用指针

void setRandom(Rect* r)
{
    int posX, posY, height, width;
    posX = rand() % 51;
    posY = rand() % 21;
    height = 2 + rand() % 11;
    width = 2 + rand() % 11;

    height = height / 2;
    width = width / 2;

    r->min.x = posX - width;
    r->min.y = posY - height;
    r->max.x = posX + width;
    r->max.y = posY + height;
}

此方法也不与this对象进行交互,因此可以将它们声明为static或移出类。