gpt4 book ai didi

c++ - 将 C++ 中的结构转换为 Ruby

转载 作者:行者123 更新时间:2023-11-28 00:47:05 24 4
gpt4 key购买 nike

我正在尝试转换这段代码:

#pragma once
#include "thread.h"
#include <vector>

struct Process {
enum Type {
SYSTEM,
USER
};

// process ID
int pid;

// process type
Type type;

// threads belonging to this process
std::vector<Thread*> threads;

// constructor
Process(int pid, Type type) : pid(pid), type(type) {}
};

进入 Ruby,但我无法弄清楚。我试过使用模块,但发现模块中实际上不能有构造函数。我也不太了解 ruby​​ struct 类。如果有人可以解释这些或帮助我转换它,将不胜感激。

最佳答案

我认为这可能值得一看:

C++ - struct vs. class

您的结构是大多数语言(包括 Ruby)所称的类(不是 C 风格的结构):

class Process
def initialize(pid, type)
@type = type
@pid = pid
@threads = []
end
attr_accessor :type, :pid, :threads
end

您需要 attr_accessor 来使成员公开(这是 C++ 中结构的默认行为)。

关于c++ - 将 C++ 中的结构转换为 Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com