我有一个队列类,其中我试图动态分配3个"示例"对象并将它们放入队列,然后出列并删除它们.但是样本对象的析构函数:
~Sample() { cout << "Destructing Sample object " << id << "\n"; }
Run Code Online (Sandbox Code Playgroud)
由于某种原因,当我尝试将对象放在队列中时被调用.
int main()
{
Sample *samp1;
Sample *samp2;
Sample *samp3;
Queue<Sample> sQa(3);
samp1 = new Sample(1);
samp2 = new Sample(2);
samp3 = new Sample(3);
cout << "Adding 3 Sample objects to queue...\n";
sQa.put(*samp1);
sQa.put(*samp2);
sQa.put(*samp3);
cout << "Dequeing and destroying the objects...\n";
for(int i = 0; i < 3; ++i)
{
delete &sQa.get();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
Adding 3 sample objects to queue...
Destructing Sample …Run Code Online (Sandbox Code Playgroud) 我在这里有一个类的头规范:
#ifndef FIXEDWINGAIRCRAFT_H
#define FIXEDWINGAIRCRAFT_H
#include <iostream>
class FixedWingAircraft
{
private:
struct Airframe
{
double weight;
};
struct Engine
{
double weight;
double fuel;
};
struct Radio
{
bool state;
double weight;
};
struct Pilot
{
int proficiency;
double weight;
};
public:
void setAirframe(double w)
{
Airframe.weight = w;
}
void setEngine(double w, double f)
{
Engine.weight = w;
Engine.fuel = f;
}
void setRadio(bool s, double w)
{
Radio.state = s;
Radio.weight = w;
}
void setPilot(int p, double …Run Code Online (Sandbox Code Playgroud) c++ ×2