![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 19
| extern const for memory allocation? There are two files of interest. File 1 : Iface.h Code: extern "C"
{
extern const IID IID_IX
}
Code: #include <objbase.h>
extern "C"
{
extern const IID IID_IX = {0x32bb8320, 0x41b, 0x11cf, ...};
// The extern is required to allocate memory for C++ constants
}
Here, Iface.h is created to "declare" IID_IX variable, and GUIDS.cpp is created to "define" the variable. So I understand why extern key word is used for the variable in Iface.h, but I don't undersatnd why it is used in GUIDS.cpp. So I turned to the comment below the definition of the variable in the GUIDS.cpp file, but I still couldn't make much sense out of it. Particularly, I didn't know what it meant when it stated that extern is requried to allocate memory for C++ constants. I would appreciate for some clarifications. Thank you. Last edited by donglee; 11-03-2008 at 01:27 AM. |
| donglee is offline | |
| | #2 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| The comment is bogus. In C++, a const definition at global scope has internal linkage by default - which means it's only available to the translation unit (source file) in which it's defined. Slapping an extern on it gives it external linkage. So a better comment would have been: // extern needed since const globals have internal linkage gg |
| Codeplug is online now | |
![]() |
| Tags |
| const, extern, guid, memory |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Inheritance Hierarchy for a Package class | twickre | C++ Programming | 7 | 12-08-2007 04:13 PM |
| matrix class | shuo | C++ Programming | 2 | 07-13-2007 01:03 AM |
| "error: incomplete type is not allowed" | Fahrenheit | C++ Programming | 9 | 05-10-2005 09:52 PM |
| Half-life SDK, where are the constants? | bennyandthejets | Game Programming | 29 | 08-25-2003 11:58 AM |
| vector<> | teval | C++ Programming | 11 | 08-18-2003 03:27 PM |