c++ - Redefined variable in the other module -
Why do we redefine the variable in other modules, so it is OK to add this module together? I have written two modules and I am now compiling and adding and it's okay. But I hope the compilation will be successful, while the linker will throw an exception like UPD: I understand that if we use The main variable in the function is a local variable of i function. It is not visible and is present outside the function is. Consider the following example Local variables have no relation. main.cpp and
module.cpp as the following:
// - main.cpp-- // # include & lt; Stdio.h & gt; Int main () {int i = 5; Printf ("I value is% d", i); }
// - module.cpp - // int i = 7;
g ++ as the following:
g ++ -c main .cpp g ++ - c module. cao g ++ - o bin main.o module.o
redefining varaible when I'm running
.bin output then the value of
i is 5 .
I as global variable module
main.cpp that linker will throw an error.
#include & lt; Iostream & gt; Int i = 3; Int main () {int i = 10 std :: cout & lt; & Lt; "I =" & lt; & Lt; I & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; ":: I =" & lt; & Lt; :: I & lt; & Lt; Std :: endl; I = :: i; Std :: cout & lt; & Lt; "I =" & lt; & Lt; I & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; ":: I =" & lt; & Lt; :: I & lt; & Lt; Std :: endl; {Int i = 20; Std :: cout & lt; & Lt; "I =" & lt; & Lt; I & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; ":: I =" & lt; & Lt; :: I & lt; & Lt; Std :: endl; } Std :: cout & lt; & Lt; "I =" & lt; & Lt; I & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; ":: I =" & lt; & Lt; :: I & lt; & Lt; Std :: endl; }
Comments
Post a Comment