所以我有以下课程
class Community { private: char* Name; char foundationDate[11]; Person* founder; int maxMembersCount; int membersCount; Person* members; static int communitiesCount;
......
我想实现一个复制构造函数:
Community::Community(const Community& other)
{
this->Name = new char[strlen(other.Name)+1];
strcpy(this->Name,other.Name);
strcpy(this->foundationDate,other.foundationDate);
this->founder = other.founder;
this->maxMembersCount = other.maxMembersCount;
this->membersCount = other.membersCount;
this->members = new Person[this->maxMembersCount];
this->members = other.members;
communitiesCount++;
}
但是每当我说 Community A=B; 时,这段代码就会崩溃;所以对我来说,这段代码似乎是合法的,但是当我开始调试时,会出现消息:this->“无法读取内存”。如果您需要更多代码示例,请帮助我,请告诉我。
Community::Community(const char* name , char foundDate[],Person* founder,int maxMembers) {
this->Name = new char[strlen(name)+1];
strcpy(this->Name,name);
strcpy(this->foundationDate,foundDate);
this->founder = new Person(founder->getName(),founder->getEGN(),founder->getAddress());
this->maxMembersCount = maxMembers;
this->membersCount = 2;
this->members = new Person[this->maxMembersCount];
communitiesCount++;
}
这是类的主要构造函数,它工作得很好......