コンテンツにスキップ

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はポインタを扱うため、安全でないとみなされます。