最多一次:指向任何特定对象的指针在任何时间点最多只能存在于一个容器对象中。
存在性:一个对象必须在插入指向它的指针之前被动态分配。
所有权:一旦插入指向对象的指针,该对象就成为容器的属性。其他人不得以任何方式使用或修改它。
守恒:当指针从容器中移除时,必须将指针插入到某个容器中,或者必须删除其所指对象。
这些是我给出的相应定义。我无法理解其中每个术语中使用的各种术语,我希望这里有人可以准确解释每个术语的含义以及如何使用它们来确保良好的编程实践。
最佳答案
例如,假设您有以下类定义:
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
};
At-most-once: A pointer to any specific object can exist in at most one container object at any point in time.
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
Existence: An object must be dynamically allocated before a pointer to it is inserted.
动态分配对象通常意味着使用运算符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;
Ownership: Once a pointer to an object is inserted, that objects becomes property of the container. No one else may use or modify it in any way.
当一个对象存在于内存中时,任何具有指向该对象的指针或引用的对象或函数都可能修改它(除非指针或引用被声明为 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
Conservation: When a pointer is removed from a container, either the pointer must be inserted into some container, or its referent must be deleted.
这意味着当容器不再想“拥有”(定义与上面完全相同的含义)它指向的项目实例时,它必须将“所有权”转移给另一个容器,或者删除该项目。对 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
}
};
关于c++ - 有人可以解释最多一次不变和存在、所有权和保护规则是什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774556/