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

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

我环顾四周,找不到我问题的答案。我正在尝试使用来自基类指针数组的派生类的复制构造函数。我学到的唯一一件事是我可能应该使用 dynamic_cast 但无法正常工作。

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

编辑:我这样做时收到的错误是 |26|error: cannot dynamic_cast '& properties[0]' (of type 'class Property**') to type 'class Commercial*' (source is not a指向类的指针)|

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

商业.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

我希望这是足够的细节

最佳答案

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

这是类型转换properties[0]Commercial* .但这不是您的复制构造函数的签名。因此,您需要 new Commercial(*dynamic_cast<Commercial*>(properties[0])); .

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

但是,一般来说,如果您使用 dynamic_cast这可能意味着您不确定派生类型是什么,您需要检查 NULL (即转换失败)在取消引用之前。

备选

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

class Base
{
public:
    virtual ~Base() = default;
    Base* clone() const = 0;
};

class D1 : public Base
{
public:
    virtual ~D1() override = default;
    D1* clone() const { return new D1(*this); }
};

class D2 : public Base
{
public:
    virtual ~D2() override = default;
    D2* clone() const { return new D2(*this); }
};

int main()
{
    std::unique_ptr<Base> b1(new D2());
    std::unique_ptr<Base> b2(b1->clone());

    return 0;
}

关于c++ - 从基指针复制派生类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641010/

相关文章:

c++ - 无法使用模板让此类工作吗?

c++ - 为什么我的驱动程序只读取字符串的一部分?

c++ - 为什么 std::cout 将 volatile 指针转换为 bool?

C - 函数内的数组初始化

java - ClassCastException 使用 Class.cast 使用泛型

java - 什么是类型转换?

c++ - 在不使用 getline 的情况下在 C++ 中读取 stdin

c++ - 如何使用 VS2017 编译旧的 C++ 标准?

c - 将函数指针作为参数传递时出错

在 Windows 中将 __int64 转换为 long