C 函数修饰/多态
有什么方法可以在所有操作类型的基本函数中执行代码吗?我想执行所有 do_card 操作通用的行为。换句话说,我想打印游戏状态,但我想避免在每个单独的 do_card 函数中重复 printf,而是只写一次。有没有办法在C中实现这个?
struct CARD {
int value;
int cost;
// This is a pointer to a function that carries out actions unique
// to this card
int (*do_actions) (struct GAME_STATE *state, int choice1, int choice2);
};
int do_card0(struct GAME_STATE *state, int choice1, int choice2)
{
// Operate on state here
}
int do_card1(struct GAME_STATE *state, int choice1, int choice2)
{
// Operate on state here
}
static struct cardDefinitions[] = {
{0, 1, do_card0},
{1, 3, do_card1}
};
int result = cardDefinitions[cardNumber].do_action(state, choice1, choice2);
Is there some way I can execute code in the base function for all action types? I want to execute behavior common to all do_card actions. In another words, I want to print the game state, but I want to avoid duplicating a printf in every individual do_card function, and instead write it once. Is there a way to implement this in C?
struct CARD {
int value;
int cost;
// This is a pointer to a function that carries out actions unique
// to this card
int (*do_actions) (struct GAME_STATE *state, int choice1, int choice2);
};
int do_card0(struct GAME_STATE *state, int choice1, int choice2)
{
// Operate on state here
}
int do_card1(struct GAME_STATE *state, int choice1, int choice2)
{
// Operate on state here
}
static struct cardDefinitions[] = {
{0, 1, do_card0},
{1, 3, do_card1}
};
int result = cardDefinitions[cardNumber].do_action(state, choice1, choice2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你真的想要模拟多态性,你可以,但它只会变得丑陋。
此时,您正在执行 C++ 编译器在幕后执行的大部分操作,您也可以只使用 C++。
If you really want to emulate polymorphism, you can, it just gets ugly.
At this point you are doing most of what the C++ compiler does behind the scenes, you may as well just use C++.
是否有什么因素阻止您简单地创建诸如
print_game_state(GAME_STATE *)
之类的子例程并在do_card
函数中使用它们?如果这不能解决您的问题,则为类之间不同的每张卡行为创建函数,并使用外部函数中的这些函数。
Is there something stoping you from simply creating subroutines like
print_game_state(GAME_STATE *)
and using them in thedo_card
functions?If this doesn't solve your problem, then create functions for each piece of card behaviour that varies between the classes and use these from the outer functions.
如果我正确理解这个问题,您是否可以简单地创建一个采用一组特定属性的通用打印函数,并从每个
do_cardX
函数调用该通用打印函数?C 中没有模板,因此您无法创建通用的单个
do_card()
函数来在函数调用时实例化其参数类型,即使可以,您也会仍然创建某种类型的专用打印函数,每个函数都必须调用该函数,以便从每个特定类型的do_card()
函数打印您想要的特定游戏状态信息。If I'm understanding this question correctly, can you not simply create a common printing function that takes a certain set of attributes, and call that common print function from each of the
do_cardX
functions?There are no templates in C, so you can't create a generic single
do_card()
function that would instantiate its argument types at the point of the function's call, and even if you could, you would have to still create some type of specialized print function that each function would have to call in order to print the specific game-state information you're wanting from each specific type ofdo_card()
function.不要在每张卡上调用 do_action,而是将卡与其他参数一起传递给您定义的另一个函数,让其调用 do 操作方法,然后调用打印状态方法或其他方法,
例如
肯定是错误,我的 C 生锈了,但这就是我认为的要点。
rather than calling do_action on each card, pass the card along with the other params to another function you define, have that call the do action method, then call the print state method or whatever
e.g.
bound to be errors my C is rusty, but that's the jist I think.