有人能解释一下最多一次不变和存在、所有权和守恒规则是什么吗?

Can someone explain what the at-most-once invariant and Existence, Ownership, and Conservation rules are?

本文关键字:所有权 存在 是什么 规则 一次 一下 能解释      更新时间:2023-10-16

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

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

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

守恒:从容器中删除指针时,必须将指针插入到某个容器中,或者必须删除其引用。

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

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

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
};
最多

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

如果有两个Container实例,并且指针指向同一Item实例,则会违反这一点:

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):

Item *pItem = new Item();  // the function containing this code obviously has a pointer
// to the newly created item
Container c(pItem);        // let the container c own this item
pItem->change();         // WRONG: attempt to modify an object owned by c

守恒:从容器中删除指针时,必须将指针插入到某个容器中,或者必须删除其引用。

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

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
}
};