C++ 在C+;中作为参数传递的Typename+;
我的朋友给了我一段代码要求解释:C++ 在C+;中作为参数传递的Typename+;,c++,macros,C++,Macros,我的朋友给了我一段代码要求解释: 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); } 我理解一个大概的想法,但我无法想象分配函数的参数
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);
}
我理解一个大概的想法,但我无法想象分配函数的参数。我想这个密码是:
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;
}
你知道分配应该是什么样子吗
显然是(Marco A.和dolan的代码):
我最好的猜测是宏:
#define ALLOCATION(type) (new type)
type_bia_motor *constructor()
{
return ALLOCATION(type_bia_motor);
}
虽然这是一种可怕的编码风格和实践,但它肯定会起作用
免责声明:让这只是一个思考练习,不要为一个严肃的项目编写这样的代码。可能是一个宏,任何事情都可能发生。。我们应该猜吗?是的!宏是个好主意!我只是想知道它是如何实现的。这是C++吗?猜测
#定义分配(X)(新X)
。顺便说一句,这太糟糕了。是的,它起作用了。“分配(X)(新X)”。实际上我从来没有用过宏。谢谢大家!
#define ALLOCATION(type) (new type)
type_bia_motor *constructor()
{
return ALLOCATION(type_bia_motor);
}