I'm new with c++ and came across to this code:
File.h
namespace Type
{
typedef BOOL (WINAPI *TYCopyFile)
(
PCHAR lpExistingFileName,
PCHAR lpNewFileName,
BOOL bFailIfExists
);
}
namespace Func
{
extern Types::TYCopyFile pCopyFileA;
}
File.cpp
namespace Funcs
{
Types::TYCopyFile pCopyFileA;
}
void Init
{
Funcs::pCopyFileA = (Types::T_CopyFile) GetProcAddress(hKernel32, "CopyFileA");
}
The idea is real simple. I have namespace of typedef
(Types
) and create function pointer in another namespace(Funcs
) as extern. Then I define that function pointer in File.cpp
in Init
function.
The question that I have is that why do I need to redeclare namespace Funcs
in File.cpp
? Why can't I just have Init
function which would initialize Funcs::pCopyFileA
? As I understand extern
, it tells compiler that the variable exists somewhere and tell linker to find it. Why can't linker find it without namespace Funcs
in File.cpp
?