有一个家庭作业,我应该在其中创建一个指向对象的指针 vector
稍后在降低负载时,我将使用继承/多态性来扩展该类以包括两天交付、次日空运等的费用。但是,这不是我现在关心的问题。当前程序的最终目标是仅打印出 vector 中每个对象的内容(名称和地址)并找出其运费(重量*成本)。
我的问题不在于逻辑,我只是对与对象/指针/vector 相关的几点感到困惑。但首先是我的代码。我基本上删除了现在不重要的所有内容,int main,将有用户输入,但现在我硬编码了两个示例。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Package {
public:
Package(); //default constructor
Package(string d_name, string d_add, string d_zip, string d_city, string d_state, double c, double w);
double calculateCost(double, double);
~Package();
private:
string dest_name;
string dest_address;
string dest_zip;
string dest_city;
string dest_state;
double weight;
double cost;
};
Package::Package()
{
cout<<"Constucting Package Object with default values: "<<endl;
string dest_name="";
string dest_address="";
string dest_zip="";
string dest_city="";
string dest_state="";
double weight=0;
double cost=0;
}
Package::Package(string d_name, string d_add, string d_zip, string d_city, string d_state, string r_name, string r_add, string r_zip, string r_city, string r_state, double w, double c){
cout<<"Constucting Package Object with user defined values: "<<endl;
string dest_name=d_name;
string dest_address=d_add;
string dest_zip=d_zip;
string dest_city=d_city;
string dest_state=d_state;
double weight=w;
double cost=c;
}
Package::~Package()
{
cout<<"Deconstructing Package Object!"<<endl;
delete Package;
}
double Package::calculateCost(double x, double y){
return x+y;
}
int main(){
double cost=0;
vector<Package*> shipment;
cout<<"Enter Shipping Cost: "<<endl;
cin>>cost;
shipment.push_back(new Package("tom r","123 thunder road", "90210", "Red Bank", "NJ", cost, 10.5));
shipment.push_back(new Package ("Harry Potter","10 Madison Avenue", "55555", "New York", "NY", cost, 32.3));
return 0;
}
所以我的问题是:
- 有人告诉我必须使用 vector 对象指针,而不是对象。 为什么?我的任务需要它 具体来说,但我也被告知 否则将无法工作。
- 我应该在哪里创建这个 vector ? 它应该是我包裹的一部分吗 类(class)?我该如何去添加 然后将对象放入其中?
我需要复制构造函数吗?为什么?
解构的正确方法是什么 我的对象指针 vector ?
如有任何帮助,我们将不胜感激。我在这里搜索了很多相关文章,我意识到我的程序会有内存泄漏。使用 boost::中的一个专用 ptrs 将无法供我使用。现在,我更关心的是构建我的程序的基础。这样我就可以真正专注于我需要创建的功能。
谢谢。
最佳答案
指针 vector 可以重复用于存储子类的对象:
class Person
{
public:
virtual const std::string& to_string () = 0;
virtual ~Person () { }
};
class Student : public Person
{
const std::string& to_string ()
{
// return name + grade
}
};
class Employee : public Person
{
const std::string& to_string ()
{
// return name + salary
}
};
std::vector<Person*> persons;
person.push_back (new Student (name, grade));
person.push_back (new Employee (name, salary));
person[0]->to_string (); // name + grade
person[1]->to_string (); // name + salary
理想情况下, vector 应该包含在一个类中。这使得内存管理更容易。它还有助于在不破坏现有客户端代码的情况下更改支持数据结构(此处为 std::vector
):
class PersonList
{
public:
Person* AddStudent (const std::string& name, int grade)
{
Person* p = new Student (name, grade);
persons.push_back (p);
return p;
}
Person* AddEmployee (const std::string& name, double salary)
{
Person* p = new Employee (name, salary);
persons.push_back (p);
return p;
}
~PersonList ()
{
size_t sz = persons.size ();
for (size_t i = 0; i < sz; ++i)
delete persons[i];
}
private
std::vector<Person*> persons;
};
因此我们可以将代码重写为:
{
PersonList persons;
Person* student = persons.AddStudent (name, grade);
Person* employee = persons.AddEmployee (name, salary);
student.to_string ();
employee.to_string ();
} // The memory allocated for the Person objects will be deleted when
// `persons` go out of scope here.
熟悉 Rule of Three将帮助您决定何时向类添加复制构造函数。另请阅读 const correctness .
关于c++ - 对象指针 vector ,一般帮助和混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6502071/