Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plain Constant variables in C++

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.

like image 595
Uchia Itachi Avatar asked Nov 23 '12 13:11

Uchia Itachi


1 Answers

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.

like image 60
SomeWittyUsername Avatar answered Oct 02 '22 01:10

SomeWittyUsername