【发布时间】:2021-08-15 11:47:55
【问题描述】:
我正在使用 C++ 慢慢地为一个游戏男孩编写一个模拟器,我目前正在处理 CPU 方面的工作。我编写了一个通用函数,它接受 CPU 的任意两个寄存器并以 Word 数据类型返回。因为需要访问单个寄存器以及寄存器组合。
const Word get_word(Byte *registerOne, Byte *registerTwo)
{
return ((*registerOne << 8) | *registerTwo);
};
调用这个函数变得乏味,因为你必须指定每个寄存器
get_word(&this->registers.h, &this->registers.l)
我的问题是这样定义宏是否可行
#define get_HL() get_word(&this->registers.h, &this->registers.l)
因为现在我可以使用
get_HL()
之所以要这样做,是因为我不想创建更多只执行函数调用的私有/公共函数。
我尝试过编译,它似乎可以正常工作,因为它只是一个预处理器宏,但我不确定设计含义
编辑:
好吧,我的意思是这有明显的缺陷,你应该只做一个函数,就像做一个函数或编写一个宏一样多的工作。
const Word get_HL() { return this->get_word(&this->h, &this->l); };
这篇文章发给有相同想法的人,希望不要再犯同样的错误
【问题讨论】:
-
I don't want to create more private/public functions that just perform function calls.为什么不呢?
标签: c++ macros preprocessor