c++ - 链表中修改的数据不会反射(reflect)在内存中

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

我有两个类,即 Family.cpp 和 Child.cpp。使用家庭实例我只能添加一个最小的 child (即最后)。使用 Child 类我只能创建一个更小的 child 。这可以通过从家庭类函数 Child* getChild(unsigned int i) 中获取 child 来完成。

家庭.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_ */

家庭.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;
}

子类.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

我想当我尝试添加一个更小的 child 时,它被添加但没有加载到家庭内存中。我该如何解决这个问题?

最佳答案

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

你应该调用 addChild 它调用 addYoungerSibling...

还有你在 C++ 中,使用字符串而不是字符(当你只能返回一个字符串时为什么要使用 strcpy?) 您可以在代码中修复各种问题。如果您上传 h 文件,如果您愿意,会更容易提供帮助。

祝你好运

关于c++ - 链表中修改的数据不会反射(reflect)在内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27188938/

相关文章:

c++ - OpenGL 和 QtQuick 纹理问题

c++ - 将 HID 触摸设备与 Pnp 监视器相关联

c++ - C++初始化中的 "several values"是什么?

c++ - 位置未处理的异常:Microsoft C++ 异常:内存位置的 std::length_error

c++ - 返回 NOT NULL 指针的正确方法是什么

c++ - boost iostream问题

java - Java 中的整数类

java - 为什么我的接口(interface)类型化对象不会执行接口(interface)中未声明的方法?

c - 为什么C中的这个简单程序崩溃(数组VS指针)

c - 如何修改已传递给 C 函数的指针?