如何在C中获得Ruby对象的类?

时间:2022-10-30 07:27:22

I have a function that handles two types: NVector and NMatrix; the former is derived from the latter. This function is basically a specialized copy constructor. I want it to return an object of the same type as that upon which it was called, so, NVector returns NVector, not NMatrix.

我有一个函数可以处理两种类型:NVector和NMatrix;前者源自后者。这个函数基本上是一个专门的复制构造函数。我希望它返回一个与它被调用的相同类型的对象,所以,NVector返回NVector,而不是NMatrix。

static VALUE nm_init_modifiedcopy(VALUE self) {
  // ... some code ...

  // formerly, I had cNMatrix where klass is. But it could also be cNVector!
  return Data_Wrap_Struct(klass, mark_func, delete_func, unwrapped_self_copy);
}

How do I get the class property of an object to pass into Data_Wrap_Struct?

如何让对象的类属性传入Data_Wrap_Struct?

1 个解决方案

#1


3  

Like clockwork, as soon as I ask a question on *, I find the answer.

就像时钟一样,我一问到*,我就能找到答案。

The macro is CLASS_OF.

CLASS_OF宏。

static VALUE nm_init_modifiedcopy(VALUE self) {
  // ... some code ...

  return Data_Wrap_Struct(CLASS_OF(self), mark_func, delete_func, unwrapped_self_copy);
}

#1


3  

Like clockwork, as soon as I ask a question on *, I find the answer.

就像时钟一样,我一问到*,我就能找到答案。

The macro is CLASS_OF.

CLASS_OF宏。

static VALUE nm_init_modifiedcopy(VALUE self) {
  // ... some code ...

  return Data_Wrap_Struct(CLASS_OF(self), mark_func, delete_func, unwrapped_self_copy);
}