对象指针,一般帮助和混乱的载体 [英] Vector of Object Pointers, general help and confusion

查看:86
本文介绍了对象指针,一般帮助和混乱的载体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行一项家庭作业,我将在其中创建指向对象的指针向量

Have a homework assignment in which I'm supposed to create a vector of pointers to objects

稍后,我将使用继承/多态性来扩展类,以包括两天交付,第二天空运等费用.但是,我现在不在意.当前程序的最终目标是仅打印出矢量中每个对象的内容(名称和地址),并找到其运输成本(重量*成本).

Later on down the load, I'll be using inheritance/polymorphism to extend the class to include fees for two-day delivery, next day air, etc. However, that is not my concern right now. The final goal of the current program is to just print out every object's content in the vector (name & address) and find it's shipping cost (weight*cost).

我的麻烦与逻辑不符,我只是对与对象/指针/向量有关的几点感到困惑.但是首先是我的代码.我基本上删去了目前不重要的所有内容,即int main,将有用户输入,但是现在我对两个示例进行了硬编码.

My Trouble is not with the logic, I'm just confused on few points related to objects/pointers/vectors in general. But first my code. I basically cut out everything that does not mater right now, int main, will have user input, but right now I hard-coded two examples.

#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;

}

所以我的问题是:

  1. 有人告诉我我必须使用向量 对象指针,而不是对象. 为什么?我的任务要求它 具体来说,但我也被告知 否则将无法正常工作.
  2. 我应该在哪里创建这个 向量? 它应该作为我的包裹的一部分吗 班级?我该如何添加 那么有物体吗?
  3. 我需要复制构造函数吗?为什么?

  1. I'm told I have to use a vector of Object Pointers, not Objects. Why? My assignment calls for it specifically, but I'm also told it won't work otherwise.
  2. Where should I be creating this vector? Should it be part of my Package Class? How do I go about adding objects into it then?
  3. Do I need a copy constructor? Why?

解构的正确方法是什么 我的对象指针向量?

What's the proper way to deconstruct my vector of object pointers?

任何帮助将不胜感激.我在这里搜索了很多相关文章,并且我意识到我的程序将发生内存泄漏.我无法使用boost ::中的一种专用ptrs.现在,我更关心建立程序基础的问题.这样,我就可以真正掌握需要创建的功能了.

Any help would be appreciated. I've searched for a lot of related articles on here and I realize that my program will have memory leaks. Using one of the specialized ptrs from boost:: will not be available for me to use. Right now, I'm more concerned with getting the foundation of my program built. That way I can actually get down to the functionality I need to create.

谢谢.

推荐答案

指针向量可以重复用于存储子类的对象:

A vector of pointers can be reused for storing objects of sub-classes:

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<Pointer*> 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

理想情况下,矢量应包装在一个类中.这使内存管理更加容易.它还有助于在不破坏现有客户端代码的情况下更改支持数据结构(此处为std::vector)

Ideally the vector should be wrapped up in a class. This makes memory management easier. It also facilitates changing the support data structure (here an std::vector) without breaking existing client code:

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;
};

因此我们可以将代码重新编写为:

So we can re-write our code as:

{
   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.

熟悉三人制将帮助您做出决定何时将复制构造函数添加到类中.另请阅读有关常量正确性的信息.

Getting familiar with the Rule of Three will help you decide when to add a copy constructor to a class. Also read about const correctness.

这篇关于对象指针,一般帮助和混乱的载体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆