链接列表中修改的数据不会反映在内存中

时间:2014-11-28 12:42:48

标签: c++ class pointers c++11 dynamic-memory-allocation

我有两个班,即Family.cpp和Child.cpp。使用家庭的实例我只能添加一个最小的孩子(即最后)。使用Child课程,我只能创建一个直接年幼的孩子。这可以通过从族类函数Child * getChild(unsigned int i)中获取子项来完成。

Family.h

#ifndef FAMILY_H_
#define FAMILY_H_

#include "Child.h"

// This is a more advanced exercise for copy and move
// constructors.
// Here there is a system of two classes and the
// copy and move should apply to the Family so as
// to create a full copy of the Family and Child.
// You can think about the main thing that is used
// by other users as the Family and the Child is
// exposed but it is going to be used only as part
// of a Family.
//
// The Family contains several children.
// The main functions that are possible to do
// to a family is to add a (youngest) child
// and to retrieve the i-th child.
// A family also supports copy and move semantics.
class Family {
public:
    Family();
    Family(const Family&);
    ~Family();

    // Add a new youngest child with this name and this
    // dob.
    void addChild(const char* name, unsigned int dob);

    // Get the i-th child of the family
    Child* getChild(unsigned int) const;

    // Copy and move through assignment operator
    Family& operator=(const Family&);
    Family& operator=(Family&&);
private:

    void inline addChildren_(const Family&);
    void inline reallocate_();
    void inline clean_();

    Child** children_;
    unsigned int childrenCapacity_;
    unsigned int numberOfChildren_;

};

#endif /* FAMILY_H_ */

Child.h

 #ifndef CHILD_H_
 #define CHILD_H_

 // This is the child that should appear as part of the family.
 // Each child has a name and a date of birth
 // The length of the name should be unbounded
 // Each child also has pointers to the (closest) older sibling and
 // the (closest) younger sibling.
 // If there is no such sibling, then, clearly, this pointer is nullptr.
 // Each child

 class Child {
 public:

 Child();
 Child(const char* name,unsigned int dob);
 ~Child(); 

 void name(char* buffer) const;
 unsigned int dob() const;
 Child* olderSibling() const;
 Child* youngerSibling() const;

// Create a new immediate younger sibling (using dynamic memory!!!)
// If a younger sibling already exists then the new one is adopted
// and should be between the two.
// If the age order between the siblings turns out to be
// wrong the operation should fail and return null pointer.
// Notice that twins (yes, up to the millisecond) should be
// possible.
 Child* addYoungerSibling(const char* name, unsigned int dob);

// Copy and move through assignment operators
 Child& operator=(const Child& other);
 Child& operator=(Child&&);

 private:
 Child* siblingOlder_;
 Child* siblingYounger_;
 char* childName_;
 int childDob_;
 void inline copyChildName_(const Child&);
 void clean_();
 };

 #endif /* CHILD_H_ */

Family.cpp

#include "Family.h"

Family::Family() :
    children_(nullptr), childrenCapacity_(0), numberOfChildren_(0) {
}

Family::~Family() {
  clean_();
}

void Family::addChild(const char* name, unsigned int dob) {

if ((numberOfChildren_ > 0)
        && (children_[numberOfChildren_ - 1]->dob() >= dob)) {
    return;
}

if (numberOfChildren_ == childrenCapacity_) {
    reallocate_();
}

if (numberOfChildren_ == 0) {
    children_[numberOfChildren_] = new Child(name, dob);
} else {
    children_[numberOfChildren_] =
            children_[numberOfChildren_ - 1]->addYoungerSibling(name, dob);
}

++numberOfChildren_;
}

Child* Family::getChild(unsigned int i) const {
if (i < numberOfChildren_) {
    return children_[i];
}
return nullptr;
}

void Family::reallocate_() {
Child** tempChildren = new Child*[childrenCapacity_ + 5];
for (unsigned int i = 0; i < childrenCapacity_ + 5; i++) {
    tempChildren[i] = (i < childrenCapacity_ ? children_[i] : nullptr);
}

if (children_) {
    delete[] children_;
}
children_ = tempChildren;
childrenCapacity_ += 5;
}

Child.cpp

 #include <string.h>

 #include "Child.h"

 Child::Child() :
    siblingOlder_(nullptr), siblingYounger_(nullptr), childName_(nullptr), childDob_(
            0) {
 }

 Child::Child(const char* name, unsigned int dob) :
    siblingOlder_(nullptr), siblingYounger_(nullptr), childName_(
            new char[strlen(name) + 1]), childDob_(dob) {
 unsigned int i = 0;
 for (; name[i]; i++) {
    childName_[i] = name[i];
 }
 childName_[i] = 0;
 }

 Child::~Child() {
      clean_();
 }

 void Child::name(char* buffer) const {
     strcpy(buffer, childName_);
 }

 unsigned int Child::dob() const {
     return childDob_;
 }

 Child* Child::olderSibling() const {
     return siblingOlder_;
 }

 Child* Child::youngerSibling() const {
    return siblingYounger_;
 }

 Child* Child::addYoungerSibling(const char* name, unsigned int dob) {

     if ((!siblingYounger_) && (childDob_ < dob)) {
         Child *temp = new Child(name, dob);
         siblingYounger_ = temp;
         temp->siblingOlder_ = this;
         return temp;
     } else if (siblingYounger_) {
         int youngerDob = youngerSibling()->dob();
         if (dob > childDob_ && dob < youngerDob) {
             Child *temp = new Child(name, dob);
             temp->siblingYounger_ = siblingYounger_;
             temp->siblingOlder_ = this;
             siblingYounger_->siblingOlder_ = temp;
             siblingYounger_ = temp;
        return temp;
    }
}
return nullptr;
 }

 void Child::copyChildName_(const Child& other) {
     unsigned int i = 0;
     for (; other.childName_[i]; i++) {
         childName_[i] = other.childName_[i];
     }
    childName_[i];
 }

问题:

我在main.cpp中有以下代码,我得到了如下输出,这是可以接受的。

 Family f;
 f.addChild("A", 5);
 f.addChild("B", 10);
 f.addChild("D", 20); 
 printFamily(f);

输出:

 Members: A 5, B 10, D 20,
 A is older than B
 B is younger than A and is older than D
 D is younger than B

但是当我将以下代码附加到main.cpp时,输出如下所示。

 Child *childB = f.getChild(1);
 Child *childC = childB->addYoungerSibling("C", 15);
 printFamily(f);

输出:

 Members: A 5, B 10, D 20,
 A is older than B
 B is younger than A and is older than C
 D is younger than C

我想当我尝试添加一个年幼的孩子时,它会被添加但不会加载到家庭记忆中。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

也许你没有把他加入家庭? 没有添加到孩子_ []?

您应该调用addChild调用addYoungerSibling ...

也是你的c ++,使用字符串而不是字符(为什么strcpy只能返回一个字符串?) 您可以在代码中修复所有类型的内容。如果您上传h文件,如果您愿意,将更容易提供帮助。

祝你好运