给定一个函数原型和一个类型定义:
int my_function(unsigned short x);
typedef unsigned short blatherskite;
以下情况是否按标准定义:
int main(int argc, char** argv) {
int result;
blatherskite b;
b=3;
result = my_function(b);
}
我是否可以通过函数原型可预测地获得类型强制?
如果你的问题真的是关于参数和参数的类型是否匹配,那么答案是肯定的。typedef
不会引入新类型,它只会为现有类型创建别名。变量b
有 type unsigned int
,就像参数一样,即使b
是使用 typedef-name 声明的blatherskite
。
不过,您的示例不太适合证明这一点。所有整数类型在 C++ 中都可以相互转换,因此(忽略范围问题)即使blatherskite
指定了不同的类型(新类型),代码也会定义行为。但事实并非如此。所以这也是完全有效的
void foo(unsigned int* p);
...
blatherskite *pb = 0;
foo(pb); // <- still valid
不需要类型强制。typedef 只是同一类型的别名,因此您将 an 传递unsigned short
给采用unsigned short
.