从基指针复制派生类的构造函数

时间:2015-04-15 03:16:23

标签: c++ pointers casting copy-constructor derived-class

我四处寻找,无法在任何地方找到我的问题的答案。我试图从基类的指针数组使用派生类的复制构造函数。我唯一学到的东西是我应该使用dynamic_cast但不能让它工作。

到目前为止,这是我的代码的重要部分(因为我有16个不同的文件,所以原来很大,但这应该足够了。)

编辑:我收到这样做的错误是| 26 |错误:无法动态播放'&属性[0]'(类型'属性**')类型'class Commercial *'(source不是指向类的指针)|

#include "rentals.h"
#include "commercial.h"
#include "sales.h"
#include "comSales.h"
#include "resSales.h"
#include "resRentals.h"
#include "comRentals.h"

const int MAX_PROPERTIES = 5;

int main(void) {
   int i;


   Property *properties[MAX_PROPERTIES];


   properties[0] = new Commercial("Notting Hill McDonalds", "4 Gardiner Road",
                                  "Notting Hill", 5000, "Li3000");


   properties[1] = new ResRentals("Janet Dalgleish", "30 Firhill Court",
                           "Mary Hill", 4000, 500.00, 300.00, 4);


   properties[2] = new Commercial(dynamic_cast<Commercial*>(properties[0]));  // <-- the copy constructor I can not get to work.


   delete[] properties;

   return 0;
}

commercial.cpp文件

#include "property_a.h"
#include "commercial.h"


Commercial::Commercial() : Property() {
   owner = "NULL";
   address = "NULL";
   suburb = "NULL";
   postcode = 0;
   license = "NULL";
}

Commercial::Commercial(string theOwner, string theAddress,
                        string theSuburb, int thepostCode,
                        string theLicense): Property(theOwner, theAddress,
                        theSuburb, thepostCode), license(theLicense) {}

Commercial::~Commercial() {}

Commercial::Commercial(const Commercial& orig) : Property(orig),
                        license(orig.getLicense()) {}

void Commercial::print() {
   cout << getOwner() << endl;
   cout << getAddress() << endl;
   cout << getSuburb() << endl;
   cout << getPostcode() << endl;
   cout << getLicense() << endl;
}

commercial.h文件

#ifndef __COMMERCIAL_H__
#define __COMMERCIAL_H__


#include "property_a.h"

class Commercial :  public virtual Property
{
protected:
  string license;

public:
  Commercial();
  Commercial(string theOwner, string theAddress, string theSuburb,
              int thepostCode, string theLicense);

   ~Commercial() ;

   Commercial(const Commercial& orig);

  void input() ;   // Data input for a Shop object
  void print() ;  // Data output for a Shop object

  string getLicense() const {return license;};   //Note the use of const

  void setLicense(string theLicense) {license = theLicense;};

};

property_a.cpp文件

#include "property_a.h"

Property::Property(){
   owner = "NULL";
   address = "NULL";
   suburb = "NULL";
   postcode = 0;
}

Property::Property(string theOwner, string theAddress,
                   string theSuburb, int thepostCode):
                     owner(theOwner), address(theAddress),
                     suburb(theSuburb), postcode(thepostCode){}

Property::~Property() {}

Property::Property(const Property& orig) :
                     owner(orig.getOwner()), address(orig.getAddress()),
                     suburb(orig.getSuburb()), postcode(getPostcode()) {}

property_a.h文件

#ifndef __PROPERTY_A_H__
#define __PROPERTY_A_H__


/*TODO  REQUIRED HEADER FILES AND NAMESPACES*/
#include <string>
#include "utility1.h"

class Property
{
protected:
  string owner;
  string address;
  string suburb;
  int postcode;

public:
  Property();
  Property(string theOwner, string theAddress, string theSuburb, int thepostCode);
  virtual ~Property();
  Property(const Property& orig);
  virtual void input() ;   // Data input for a Property object
  virtual void print() ;  // Data output for a Property object

  string getOwner() const {return owner;};   //Note the use of const
  string getAddress() const {return address;};
  string getSuburb() const {return suburb;};
  int getPostcode() const {return postcode;};

  void setOwner(string newOwner) {owner = newOwner;};
  void setAddress(string newAddress) {address = newAddress;};
  void setSuburb( string  newSuburb) {suburb = newSuburb;};
  void setPostcode(int  newPostcode) {postcode = newPostcode;};
};
#endif

我希望这是足够的细节

2 个答案:

答案 0 :(得分:1)

看到错误会很高兴,但看起来你根本没有调用复制构造函数:

new Commercial(dynamic_cast<Commercial*>(properties[0]));

就像打电话

Commercial(Commercial * other);

所以你需要

new Commercial(*dynamic_cast<Commercial*>(properties[0]));

答案 1 :(得分:1)

  

properties [2] = new Commercial(dynamic_cast(properties [0])); //&lt; - 复制构造函数我无法工作。

这是将properties[0]投射到Commercial*。但这不是复制构造函数的签名。因此,您需要new Commercial(*dynamic_cast<Commercial*>(properties[0]));

在此示例中,您可以使用static_cast<Commercial&>(*properties[0]),因为您 知道 properties[0]Commercial类型。

,一般情况下,如果您使用dynamic_cast,则可能意味着您不确定派生类型是什么,您需要检查{{在解除引用之前1}}(即,转换失败)。

替代

你可以考虑使用多态API来为你处理这个问题。

NULL