google protocol-buffers c++ repeated 链表结构释放内存空间

来源:互联网 发布:淘宝要不要开企业店铺 编辑:程序博客网 时间:2024/05/22 01:33

在使用 Repeated 类型 链表结构 使用add_foo() 以后,要注意clear_foo() 释放内存空间,最好的办法是在该链表类的析构方法中执行 this->clear_foo();

Repeated Embedded Message Fields

Given the message type:

message Bar {}

For this field definitions:

repeated Bar foo = 1;

The compiler will generate the following accessor methods:

  • int foo_size() const: Returns the number of elements currently in the field.
  • const Bar& foo(int index) const: Returns the element at the given zero-based index.
  • Bar* mutable_foo(int index): Returns a mutable pointer to the Bar object that stores the value of the element at the given zero-based index. The pointer is invalidated by a call to Clear() or clear_foo(), or by manipulating the underlying RepeatedPtrField in a way that would remove this element.
  • Bar* add_foo(): Adds a new element and returns a pointer to it. The returned Bar will have none of its fields set (i.e. it will be identical to a newly-allocated Bar). The pointer is invalidated by a call to Clear() or clear_foo(), or by manipulating the underlying RepeatedPtrField in a way that would remove this element.
  • void clear_foo(): Removes all elements from the field. After calling this, foo_size() will return zero.
  • const RepeatedPtrField<Bar>& foo() const: Returns the underlying RepeatedPtrField that stores the field's elements. This container class provides STL-like iterators and other methods.
  • RepeatedPtrField<Bar>* mutable_foo(): Returns a mutable pointer to the underlying RepeatedPtrField that stores the field's elements. This container class provides STL-like iterators and other methods.