简体   繁体   English

重载函数中枚举器的转换

[英]conversion of enumerator in overloading function

====================================Edited question=========================== I have read in a book that the conversion of enumerator depends to the number that the enumerator is initialized to. ====================================编辑问题============ ============== 我在一本书中读到,枚举器的转换取决于枚举器初始化为的数字。 for EX:in this enum the first enum must convert to int and the second one must convert to double ( because the second one initialized with a number out of range of int.)对于 EX:在此枚举中,第一个枚举必须转换为 int,第二个枚举必须转换为 double(因为第二个枚举使用超出 int 范围的数字进行初始化。)

enum name{A= INT_MAX , B};

void foo(int a)
{
  print "int prameter";
}

void foo(double a)
{
  print "double parameter";
}

foo(B) // must print double parameter, no?

well we should see "double parameter" when we call foo(B) but we will see "int parameter" instead .好吧,当我们调用 foo(B) 时,我们应该看到“双参数”,但我们会看到“int 参数”。 why is that?这是为什么? what is the way of conversion in enumerator?枚举器中的转换方式是什么?

It's a type issue.是类型问题。 int (*p)[2] is two dimensional but a[0] is one dimensional. int (*p)[2]是二维的,而a[0]是一维的。 So the two aren't compatible.所以两者不兼容。 This works though int* p = a[0];这虽然int* p = a[0];

Think about how many times you have to dereference (using * ) to get to the first integer.想想你有多少次取消引用(使用* )才能得到第一个整数。 With int (*p)[2] it's twice使用int (*p)[2]是两次

cout << **p; // print first integer

but with int a[2][2] and a[0] it's only once但是对于int a[2][2]a[0]它只有一次

cout << *(a[0]); // print first integer

but twice would be a compile error但两次将是编译错误

cout << **(a[0]); // error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM