// Strtref. cpp -- using structure references
# Include <iostream>
Using namespace STD;
Void fun (const int * num)
{
// Num = 435;
Cout <* num <", const fun" <Endl;
}
Void fun (int * num)
{
// Num = 435;
Cout <* num <", not const fun" <Endl;
}
Int main ()
{
Const int num = 999;
Const int * P = & num;
Int AVG = 123;
Int * P2 = & AVG;
// Because the fun parameter is pointer type, it can be reloaded according to const and non-Const. If void fun (INT num) and void fun (const int num)It cannot be reloaded.
Fun (p); // output "999, const fun"
Fun (P2); // output "123, not const fun"
Return 0;
}
However, note that non-const real parameters can also match const parameters, but const real parameters cannot match non-const parameters:
1. Non-const real parameters can also match const Parameters
# Include <iostream>
Using namespace STD;
Void fun (const int * num)
{
// Num = 435;
Cout <* num <", const fun" <Endl;
}
Int main ()
{
Int AVG = 123;
Int * P2 = & AVG;
Fun (P2); // output "123, const fun"
Return 0;
}
2. Const real parameters cannot match non-const parameters. The following sectionCodeIf the compilation fails, an error is reported, indicating that the const int * cannot be converted to int *"
# Include <iostream>
Using namespace STD;
Void fun (int * num)
{
// Num = 435;
Cout <* num <", const fun" <Endl;
}
Int main ()
{
Int AVG = 123;
Const int * P2 = & AVG;
Fun (P2 );
Return 0;
}