[英]Copy constructor for derived class from base pointer
I have looked all around and can't find the answer to my question anywhere. 我环顾四周,在任何地方都找不到我的问题的答案。 I am trying to use a copy constructor of a derived class from a pointer array of base classes.
我试图使用从基类的指针数组派生类的副本构造函数。 The only thing I have learned is I should probably use dynamic_cast but cant get that working.
我了解到的唯一一件事是,我可能应该使用dynamic_cast,但无法正常工作。
This is the important parts of my code so far (original is way to big since I have 16 different files but this should be enough). 到目前为止,这是我代码的重要组成部分(因为我有16个不同的文件,所以原始代码非常有用,但这已经足够了)。
EDIT: The error I receive doing it this way is |26|error: cannot dynamic_cast '& properties[0]' (of type 'class Property**') to type 'class Commercial*' (source is not a pointer to class)| 编辑:我收到的错误信息是| 26 |错误:无法dynamic_cast'&properties [0]'('class Property **'类型)键入'class Commercial *'(源代码不是指向类的指针) )|
#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 file 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 file 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 file 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 file 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
I hope this is enough details 我希望这是足够的细节
It would be nice to see the errors but it looks like you aren't calling the copy constructor at all: 很高兴看到错误,但看起来您根本根本没有调用复制构造函数:
new Commercial(dynamic_cast<Commercial*>(properties[0]));
is like calling 就像打电话
Commercial(Commercial * other);
so you need 所以你需要
new Commercial(*dynamic_cast<Commercial*>(properties[0]));
properties[2] = new Commercial(dynamic_cast(properties[0]));
properties [2] =新的Commercial(dynamic_cast(properties [0])); // <-- the copy constructor I can not get to work.
// <-复制构造函数,我无法使用。
This is casting properties[0]
to Commercial*
. 这是将
properties[0]
为Commercial*
。 But this isn't the signature of your copy constructor. 但这不是您的复制构造函数的签名。 Therefore, you need
new Commercial(*dynamic_cast<Commercial*>(properties[0]));
因此,您需要
new Commercial(*dynamic_cast<Commercial*>(properties[0]));
. 。
In this example you could use static_cast<Commercial&>(*properties[0])
since you know properties[0]
is a Commercial
type. 在此示例中,您可以使用
static_cast<Commercial&>(*properties[0])
因为您知道 properties[0]
是Commercial
类型。
However , in general if you're using dynamic_cast
it probably means you're not sure what the derived type is and you would need to check for NULL
(ie, the cast failed) before dereferencing. 但是 ,通常,如果您使用
dynamic_cast
则可能意味着您不确定派生类型是什么,并且在取消引用之前需要检查NULL
(即强制转换失败)。
You could consider a polymorphic API to take care of this for you. 您可以考虑使用多态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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.