gpt4 book ai didi

c++ - 在 C++ 中作为参数传递的类型名

转载 作者:行者123 更新时间:2023-11-28 06:40:44 27 4
gpt4 key购买 nike

我的 friend 给了我一段代码要求解释它:

typedef struct bia_motor {
unsigned int attributes123;
} type_bia_motor;

typedef struct bia {
int attributes456;
} type_bia;

type_bia_motor *constructor()
{
return ALLOCATION(type_bia_motor);
}

我明白了一个大概的意思,但是我无法想象ALLOCATION函数的参数。我认为这段代码:

type_bia_motor* ALLOCATION( ??? ) {
return new type_bia_motor;
}

更详细:

void* ALLOCATION( TYPENAME ) {
// IF type_bia_motor IS NEEDED
return new type_bia_motor;
// IF type_bia IS NEEDED
return new type_bia;
}

知道 ALLOCATION 应该是什么样子吗?

显然是(Marco A. & dolan 的代码):

ALLOCATION(X) (new X)

最佳答案

我最好的猜测是一个宏:

#define ALLOCATION(type) (new type)

type_bia_motor *constructor()
{
return ALLOCATION(type_bia_motor);
}

这绝对可行,尽管这是一种糟糕的编码风格和做法。

免责声明:让这只是一种思考练习,永远不要为一个严肃的项目编写这样的代码。

关于c++ - 在 C++ 中作为参数传递的类型名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26026918/

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