Spec-Zone.ru › Crystal

pointerof

Выражение pointerof возвращает указатель, который указывает на содержимое переменной или переменной экземпляра.

Пример с переменной:

a = 1

ptr = pointerof(a)
ptr.value = 2

a # => 2

Пример с переменной экземпляра:

class Point
  def initialize(@x : Int32, @y : Int32)
  end

  def x
    @x
  end

  def x_ptr
    pointerof(@x)
  end
end

point = Point.new 1, 2

ptr = point.x_ptr
ptr.value = 10

point.x # => 10

Поскольку pointerof использует указатели, оно считается небезопасным.

To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/reference/1.2/syntax_and_semantics/pointerof.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API