C++,不需要修改队列中的(随机)对象,对象指针
C++, modification of (random) object in a queue not desired, object pointors
我创建了一个对象指针的队列(多亏了向量库(。
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
。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 在类定义中声明随机类的对象
- 具有随机值的多个对象及其碰撞的 SFML
- 指向用随机指针归档的其他类对象的指针的c++向量
- C++ 在运行时选择一个随机对象
- 从基于属性的列表中返回随机对象
- 我使用箭头运算符打印出对象的变量值,并得到一些随机值作为输出
- C++创建相同的随机值的对象数组
- 将C 对象读取到随机访问文件中
- 随机生成对象的算法
- 将提升随机代码转换为类对象
- GCC 在被杀时创建带有随机后缀的对象文件
- 对象复制:复制随机引擎的正确方法
- OpenGL-在随机方向上移动的对象
- 对象在轨迹上的随机放置
- 在C++中创建一个具有随机枚举类型的Card对象
- 从对象阵列中选择一个随机对象
- 我是否应该保留随机分布对象实例,或者我可以总是重新创建它
- 子对象(看似)随机设置为 NULL 或"非法对象";如何调试?
- C++,不需要修改队列中的(随机)对象,对象指针