Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有人能解释一下什么是充其量的不变量以及存在、所有权和保护规则吗?_C++ - Fatal编程技术网

C++ 有人能解释一下什么是充其量的不变量以及存在、所有权和保护规则吗?

C++ 有人能解释一下什么是充其量的不变量以及存在、所有权和保护规则吗?,c++,C++,最多一次:在任何时间点,指向任何特定对象的指针最多可以存在于一个容器对象中 存在性:在插入指向对象的指针之前,必须动态分配对象 所有权:一旦插入指向对象的指针,该对象就成为容器的属性。任何其他人不得以任何方式使用或修改它 保存:从容器中删除指针时,必须将指针插入某个容器,或者删除其引用对象 这些是我得到的各自的定义。我很难理解它们中使用的各种术语,我希望这里的人能够准确地解释每一个术语的含义以及如何使用它们来确保良好的编程实践。例如,假设您有以下类定义: class Container {

最多一次:在任何时间点,指向任何特定对象的指针最多可以存在于一个容器对象中

存在性:在插入指向对象的指针之前,必须动态分配对象

所有权:一旦插入指向对象的指针,该对象就成为容器的属性。任何其他人不得以任何方式使用或修改它

保存:从容器中删除指针时,必须将指针插入某个容器,或者删除其引用对象


这些是我得到的各自的定义。我很难理解它们中使用的各种术语,我希望这里的人能够准确地解释每一个术语的含义以及如何使用它们来确保良好的编程实践。

例如,假设您有以下类定义:

class Container {
  public:
    // construct a container instance by making it point to an item
    Container(Item *pItem) : m_pItem(pItem) { }
  private:
    Item *m_pItem;
};

class Item {
  public:
    change() {
      // do something to change the item
    }
  private:
    // some private data
};

最多一次:在任何时间点,指向任何特定对象的指针最多可以存在于一个容器对象中

如果两个
容器
实例的指针指向同一个
实例,则会违反以下规定:

Item *pItem = new Item();  // construct a new Item
Container c1(pItem);       // create a new container pointing to this item
Container c2(pItem);       // WRONG: create another container pointing to the same item

存在性:在插入指向对象的指针之前,必须动态分配对象

动态分配一个对象通常意味着使用操作符
new
创建一个对象的新实例,该操作符返回一个指针,指向在堆内存上分配的新创建的对象(与堆栈相反)。违反这一规定的两个例子是:

Item *pItem = NULL;
Container c1(pItem);  // WRONG: pItem is not dynamically allocated; it is NULL

Item item;
Container c2(&item);  // WRONG: &item does not point to a dynamically allocated heap
                      // object; the object is on stack;

所有权:一旦插入指向对象的指针,该对象就成为容器的属性。任何其他人不得以任何方式使用或修改它

当一个对象存在于内存中时,任何具有指向该对象的指针或引用的对象或函数都可能修改它(除非该指针或引用声明为
const
):


保存:从容器中删除指针时,必须将指针插入某个容器,或者删除其引用对象

这意味着,当容器不再希望“拥有”(定义与上面完全相同)它所指向的项实例时,它必须将“所有权”转移到另一个容器,或者删除该项。对
容器
类的以下修改实现了此行为:

class Container {
  public:
    removeItem() {
      delete m_pItem;  // deallocate the item instance owned by this container
                       // the item is no longer owned because it no longer exists
    }
    giveItemTo(const Container& other) {
      other.m_pItem = m_pItem;  // let the other container own this item instead
      m_pItem = NULL;           // the item is no longer owned by this container because the pointer is NULL
    }
};

这些听起来像是避免内存泄漏的原则,但不完全一样。这些规则是从哪里来的?你能更具体地说你不理解哪些术语吗?它“在任何时间点最多存在于一个容器对象中”意味着什么?有什么违反这一规则或其他规则的代码示例吗?最初我认为“容器”意味着类似std::list或std::vector的东西。但cdesrosiers的解释更有意义。
class Container {
  public:
    removeItem() {
      delete m_pItem;  // deallocate the item instance owned by this container
                       // the item is no longer owned because it no longer exists
    }
    giveItemTo(const Container& other) {
      other.m_pItem = m_pItem;  // let the other container own this item instead
      m_pItem = NULL;           // the item is no longer owned by this container because the pointer is NULL
    }
};