Plain constant variables in C++ default to internal linkage. Suppose If I have the following:
I define a const variable in a header file(const int var = 2
)
Then I include the header in two cpp files.
If I try getting the address of that const variable (i.e &var
) in both of the cpp files, then will those two addresses be same? Also I need a small working code to verify this fact.
I had to post this as a question because I couldn't ask it there in the comments for this answer given in this thread as I'm a newbie.
For C++
it won't be the same due to the internal linkage - these are 2 distinct objects.
In C
it's the other way around and const
will have external linkage, thus you will get a linkage error due to redefinition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With