[英]New command not making a new object C++
I will start by explaining my issue, and then provide a good portion of the code. 我将首先解释我的问题,然后提供大部分代码。 I and filling a queue with a custom class called process.
我并用一个名为process的自定义类填充队列。 Using this line:
使用此行:
ProcessQueue.push(new Process(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]));
This all seems well and good, when the loop is done Process queue is filled with pointers. 当完成循环时,这一切似乎都很好,进程队列中充满了指针。 However upon further inspection I found out they all point to the SAME object?
但是,经过进一步检查,我发现它们都指向同一对象?
Finding this curious, I stepped into the constructor on each iteration of the loop. 对此感到好奇,我在循环的每次迭代中进入了构造函数。
First iteration: when the constructor is entered all instance variables are null (as expected)
第一次迭代:输入构造函数后,所有实例变量均为null(符合预期)
Second iteration: upon entering the constructor, all instance variables contain the values given to the object in the first iteration (ie: the same object)
第二次迭代:进入构造函数后,所有实例变量都包含第一次迭代中赋予对象的值(即:同一对象)
Furthermore later when I use the queue I have confirmed that every pointer in the queue is referring to the same process object. 此外,稍后当我使用队列时,我已经确认队列中的每个指针都指向相同的过程对象。 (I can tell this because process contains a state and if loop through the queue changing the state, I will find the state already changed for the second pointer)
(我可以告诉您,因为进程包含一个状态,并且如果循环通过队列更改状态,我会发现第二个指针的状态已经更改)
I suspect I must have done something wrong with the creation of me class. 我怀疑在创建班级上一定做错了什么。 so here it is in its entirety.
所以这里是全部。
Process.h 进程
#pragma once
class Process
{
public:
Process(int _processId, int _arrivalTime, int _CPUTime,
int _IOFrequency, int _IODuration);
~Process();
bool HasArrived(int time);
bool HasCompleted();
bool HasFinishedBurst();
bool HasFinishedIO();
int GetQueueNum();
int GetID();
void SetQueueNum(int i);
void SetToReady();
void Run();
void PerformIO();
};
Process.cpp Process.cpp
#include "stdafx.h"
#include "Process.h"
#include <string>
using namespace std;
int processId;
int arrivalTime;
int CPUTime;
int IOFrequency;
int IODuration;
int Ticks;
int CPUConsumption;
int CPUBurstSize;
int queueNumber;
int IOBurstCount;
string state;
Process::Process(int _processId, int _arrivalTime, int _CPUTime,
int _IOFrequency, int _IODuration)
{
processId = _processId;
arrivalTime = _arrivalTime;
CPUTime = _CPUTime;
IOFrequency = _IOFrequency;
IODuration = _IODuration;
IOBurstCount = 0;
CPUConsumption = 0;
Ticks = 0;
queueNumber = 0;
state = "None";
printf("%d: %s\n", processId,state.c_str());
int excess = CPUTime % IOFrequency;
if (excess == 0)
{
CPUBurstSize = CPUTime / IOFrequency;
}
else
{
CPUBurstSize = (CPUTime - excess) / (IOFrequency - 1);
}
}
Process::~Process()
{
}
bool Process::HasArrived(int time)
{
if (arrivalTime <= time)
{
if (state.compare("Newly Arrived") == 0)
{
printf("Already arrived!\n");
}
state = "Newly Arrived";
printf("%d: %s\n", processId, state.c_str());
return true;
}
else
{
return false;
}
}
bool Process::HasCompleted()
{
if (CPUConsumption == CPUTime && IOBurstCount == IOFrequency)
{
state = "Finished";
printf("%d: %s\n", processId, state.c_str());
return true;
}
else
{
return false;
}
}
bool Process::HasFinishedBurst()
{
if (Ticks == CPUBurstSize)
{
Ticks = 0;
state = "Blocked";
printf("%d: %s\n", processId, state.c_str());
return true;
}
else
{
return false;
}
}
bool Process::HasFinishedIO()
{
if (Ticks >= IODuration)
{
IOBurstCount++;
Ticks = 0;
return true;
}
else
{
return false;
}
}
void Process::SetToReady()
{
state = "Ready";
printf("%d: %s\n", processId, state.c_str());
}
void Process::Run()
{
state = "Running";
printf("%d: %s\n", processId, state.c_str());
Ticks++;
CPUConsumption++;
}
void Process::PerformIO()
{
Ticks++;
}
int Process::GetQueueNum()
{
return queueNumber;
}
void Process::SetQueueNum(int i)
{
queueNumber = i;
}
int Process::GetID()
{
return processId;
}
I suspect I have somehow created this as a static class without meaning too... 我怀疑我已经以某种方式将其创建为静态类而没有任何意义...
It seems you have placed all your member variables outside the class! 看来您已经将所有成员变量都放在了类之外!
int processId;
int arrivalTime;
int CPUTime;
int IOFrequency;
int IODuration;
int Ticks;
int CPUConsumption;
int CPUBurstSize;
int queueNumber;
int IOBurstCount;
string state;
Should be here: 应该在这里:
class Process
{
public:
Process(int _processId, int _arrivalTime, int _CPUTime,
int _IOFrequency, int _IODuration);
~Process();
bool HasArrived(int time);
bool HasCompleted();
bool HasFinishedBurst();
bool HasFinishedIO();
int GetQueueNum();
int GetID();
void SetQueueNum(int i);
void SetToReady();
void Run();
void PerformIO();
private:
int processId;
int arrivalTime;
int CPUTime;
int IOFrequency;
int IODuration;
int Ticks;
int CPUConsumption;
int CPUBurstSize;
int queueNumber;
int IOBurstCount;
string state;
};
You defined global variables 您定义了全局变量
int processId;
int arrivalTime;
int CPUTime;
int IOFrequency;
int IODuration;
and the constructor of class Process each time when it is called overrides their values. 每次调用时,Process类的构造函数都会覆盖其值。
Process::Process(int _processId, int _arrivalTime, int _CPUTime,
int _IOFrequency, int _IODuration)
{
processId = _processId;
arrivalTime = _arrivalTime;
CPUTime = _CPUTime;
IOFrequency = _IOFrequency;
IODuration = _IODuration;
IOBurstCount = 0;
CPUConsumption = 0;
Ticks = 0;
queueNumber = 0;
//...
While the class itself has no data members. 而类本身没有数据成员。
So these global variables keep that values that were assigned to them in the last call of the constructor. 因此,这些全局变量将保留在构造函数的最后一次调用中分配给它们的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.