-
overloading question
Maybe it's not overloading... I don't know what it is.
Basically:
Code:
int main() {
int random;
if (random > 3)
int value;
if (random <= 3)
float value;
add_factor(value);
}
void add_factor(int temp) {
}
void add_factor(float temp) {
}
Is this code illegal? Under what circumstances could value be used without error? (Pretend that these variables have values of some importance in them, and that add_factor(int) is different than add_factor(float).
-
Its not legal. You are declaring "value" twice in their own statement blocks and don't have scope within main(). For example, the following is equivalent:
Code:
int main()
{
int random;
if (random > 3)
{
int value; //can't access value outside of {}
}
if (random <= 3)
{
float value;
}
add_factor(value); //value not defined in this scope
return 0; //added for correctness
}
Your function declarations for add_factor() are ok. The type of the parameter will determine which function is called.
You can accomplish what [it looks like] you were thinking like this:
Code:
if (random > 3)
add_factor(<something of int type>);
if (random <= 3)
add_factor(<something of float type>);
You can use templates and template specialization to do something similiar.
gg
-