c++11 - Is there a way to hide the C++ class definition across compilation unit? -


is there anyway hide c++ class definition across compilation units?

consider,

//test1.cpp struct local {     local()     {        std::cout<<"test1::local\n";     } }; void test1() {     local l; }   //test2.cpp struct local {     local()     {         std::cout<<"test2::local\n";     } };  void test2() {     local l; }    //main.cpp void test1(); void test2(); int main() {    test1();    test2(); } 

it should link & print below,

test1::local test2::local 

i need mechanism similar static functions, don't want use namespace or anonymous namespace because still symbol information exported in object file.

you can use anonymous namespace:

namespace {     struct local     {         local()         {            std::cout<<"test1::local\n";         }     }; }  void test1() {     local l; } 

that restricts scope of name local translation unit it's used. (formally that's not correct, if think of way won't go wrong)


Comments