有人能解释一下最多一次不变和存在、所有权和守恒规则是什么吗?
Can someone explain what the at-most-once invariant and Existence, Ownership, and Conservation rules are?
最多一次:在任何时间点,指向任何特定对象的指针最多可以存在于一个容器对象中。
存在:在插入指向对象的指针之前,必须动态分配对象。
所有权:插入指向对象的指针后,该对象将成为容器的属性。任何其他人不得以任何方式使用或修改它。
守恒:从容器中删除指针时,必须将指针插入到某个容器中,或者必须删除其引用。
这些是我得到的各自定义。 我很难理解每个术语中使用的各种术语,我希望这里有人可以准确地解释每个术语的含义以及如何使用它们来确保良好的编程实践。
例如,假设您具有以下类定义:
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
}
};
相关文章:
- C++模板来检查友元函数的存在
- 既然存在危险,为什么项目要使用-I include开关
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- C++quit()函数中可能存在作用域问题
- C++擦除(如果存在)
- g++ 说函数不存在,即使包含正确的标头
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 有了gcc,是否可以链接库,但前提是它存在
- 我是否需要在下一次转移时将所有权*转移回转移队列
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- C++Builder中的OnClick事件签名存在问题
- 如何正确地将分支添加到已存在的树中
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 如何检查QList中是否存在值
- 智能指针概念所有权和寿命
- 根据某个函数是否存在启用模板
- 如何将分支添加到已存在的TTree:ROOT
- 有人能解释一下最多一次不变和存在、所有权和守恒规则是什么吗?