C++,不需要修改队列中的(随机)对象,对象指针

C++, modification of (random) object in a queue not desired, object pointors

本文关键字:对象 随机 指针 不需要 修改 队列 C++      更新时间:2023-10-16

我创建了一个对象指针的队列(多亏了向量库(。

std::vector<Polygon*>queue;

我的类Polygon是所有其他类的母类,它的基本目的是绘制形状。所以多边形是在画一个多边形,矩形,矩形。。。其中*和#都是随机选择的。它实际上非常有效。

所以我想创建几个这样的图形,并将它们放在队列中,当程序结束时显示所有形状(使用FIFO(。一开始它似乎很管用,但后来我发现这幅画与众不同。

例如,矩形仍然是一个矩形,具有相同的尺寸,但*和#的样式将不同。因此,这意味着该图形尚未保存,但其参数已保存。队列正在重建这些。

我想做的是保存相同的形状,我做错了什么?

/* 
 * File:   main.cpp
 * Author: vfortineau
 *
 * Created on may 12nd 2015, 10:25
 */
#include <cstdlib>
#include <iostream>
#include <vector>
#include "Polygon.h"
#include "Rectangle.h"
#include "Square.h"
#include "IsoscelesTriangle.h"
#include "ReversedTriangle.h"
#include "Diamond.h"
using namespace std;
/*
 * 
 */
int main(int argc, char** argv) {
    int choice;
    int _height;
    int _width;
    int _posi;
    int i=0;
    Polygon* polymorph;
    std::vector<Polygon*>queue;
    while (1)
    {
        cout << "ntPlease type one of the following command :n" << endl;
        cout << "t********************************************n"<<
                "t* 1 - Upright isosceles triangle           *n" << 
                "t* 2 - Inverted isosceles triangle          *n" << 
                "t* 3 - Rectangle                            *n" << 
                "t* 4 - Square                               *n"<< 
                "t* 5 - Diamond                              *n"<< 
                "t* 6 - Polygone                             *n" << 
                "t* 0 - Exit Program                         *n" << 
                "t********************************************" << endl;
        cin >> choice;
        switch(choice)
        {
            case 1:
                cout << "Choose the size of the height (please type an integer) : n";
                cin >> _height;
                cout << "Choose the position (please type an integer) : n ";
                cin >> _posi;
                polymorph = new IsoscelesTriangle(_height, _posi);
                polymorph->drawShape();
                queue.push_back(polymorph); 
                break;
            case 2:
                cout << "Choose the size of the height (please type an integer) : n";
                cin >> _height;
                cout << "Choose the position (please type an integer) : n ";
                cin >> _posi;
                polymorph = new ReversedTriangle(_height, _posi);
                polymorph->drawShape();
                queue.push_back(polymorph); 
                break;
            case 3:
                cout << "Choose the size of the height (please type an integer) : n";
                cin >> _height;
                cout << "Choose the size of the width (please type an integer) : n";
                cin >> _width;
                cout << "Choose the position (please type an integer) : n ";
                cin >> _posi; 
                polymorph = new Rectangle(_height, _width, _posi);
                polymorph->drawShape();
                queue.push_back(polymorph); 
                break;
            case 4:
                cout << "Choose the size of the sides (please type an integer) : n";
                cin >> _height;
                cout << "Choose the position (please type an integer) : n ";
                cin >> _posi; 
                polymorph = new Square(_height, _width, _posi);
                polymorph->drawShape(); 
                queue.push_back(polymorph); 
                break;
            case 5:
                cout << "Choose the size of the vertical diagonal (please type an integer) : n";
                cin >> _height;
                cout << "Choose the position (please type an integer) : n ";
                cin >> _posi; 
                polymorph = new Diamond(_height, _posi);
                polymorph->drawShape();
                queue.push_back(polymorph); 
                break;
            case 6:
                cout << "Choose the size of the height (please type an integer) : n";
                cin >> _height;
                cout << "Choose the size of the width (please type an integer) : n";
                cin >> _width;
                cout << "Choose the position (please type an integer) : n ";
                cin >> _posi; 
                polymorph = new Polygon(_height, _width, _posi);
                polymorph->drawShape(); 
                queue.push_back(polymorph); 
                break;
            case 0:
                while(not queue.empty())
                {
                    i++;
                    cout << endl << "t Figure " << i << endl;
                    cout << "t---------nn";
                    queue[0]->drawShape();
                    queue.erase(queue.begin());                    
                }
                return 0;
        }
    }
    return 0;
}

作为一个例子,我加入了drawShape((函数的一个版本,但我不认为问题来自这里。

void IsoscelesTriangle::drawShape()
{
    srand(time(NULL));
    int random=0;
    for(int i=0; i<height; i++)
    {
        position();
        for(int j=0; j<height-(i+1); j++)
        {
            std::cout << "  ";
        }
        for (int k=0; k<(2*i+1); k++)
        {
            random=rand()%2;
            if (random==0) std::cout << "* ";
            else std::cout << "# ";            
        }
        std::cout << std::endl;
    }
}

height是Polygon的受保护成员,并定位Polygon的公共函数。

输出的示例

for (int k=0; k<(2*i+1); k++)
{
    random=rand()%2;
    if (random==0) std::cout << "* ";
    else std::cout << "# ";            
}

每次调用drawShape()时,它都会被执行,并生成一个新的随机模式*和#。如果你想保持一致,你需要在构建对象时生成一次模式,将其存储在某个地方,并让drawShape()查看预生成的模式。

IsoscelesTriangle::drawShape()的行为是随机的。如果你在建筑上扔硬币并永远使用,就把它作为你的物品的一部分。

例如,

class IsoscelesTriangle
{
 //stuff
 private:
 bool useStar;
};

并在IsoscelesTriangle:的构造函数中初始化CCD_

IsoscelesTriangle::IsoscelesTriangle(int height, int pos)
{
 srand(time(NULL));
 useStar = (rand()%2 == 0);
}

最后,将IsoscelesTriangle::drawShape()中的random == 0检查替换为布尔值useStar