c++ - Can I avoid using the class name in the .cpp file if I declare a namespace in the header? -


this question has answer here:

in c++, want declare displayinfo class in .h file, , in .cpp file, not have type first displayinfo::displayinfo() , every function definition.

sadly, i've looked @ on 20 topics , c++ book on 2 hours , have not been able resolve this. think it's because i'm trying use 10-year-old java training in c++.

1st trial:

//displayinfo.h   namespace displayinfonamespace  {   class displayinfo    {     public:     displayinfo(); //default constructor     float getwidth();     float getheight();     ...   }; }  //displayinfo.cpp using namespace displayinfonamespace;  //doesn't work using namespace displayinfonamespace::displayinfo //doesn't work either using displayinfonamespace::displayinfo //doesn't work {   displayinfo::displayinfo() {}; //works when remove namespace, first displayinfo:: don't want type    displayinfo::getwidth() {return displayinfo::width;}  //more displayinfo:: don't want type   ... } 

for second trial, tried switching order,

class displayinfo {    namespace displayinfonamespace   {   ...   } } 

and in .cpp file, tried of above plus

using namespace displayinfo::displayinfonamespace;  

for third trial tried forward declaring header:

namespace displayinfonamespace {   class displayinfo; } class displayinfo { public: ...all methods , constructors... }; 

i'm using visualstudio2010 express , despite reading error messages have not been able find right arrangement of classes , namespaces in header , .cpp file make work out.

and after spent 30 minutes typing this, c++: "class namespaces"? answer? (aka no, have use typedefs?)

there no way shorten a::a() definition syntax, when outside of class.

within class alow define functions inplace without havinf select correct scope.

example:

// in *.h namespace meh {   class {   public:     a() {       std::cout << "a construct" << std::endl;     }      void foo();     void bar();   }    void foo(); }  void foo();   // in *.cpp  void foo() {   std::cout << "foo outside namespace" << std::endl; }  void meh::foo() {   std::cout << "foo inside namespace, not inside class" << std::endl; }  void meh::a::foo() {   std::cout << "foo" << std::endl; }   namespace meh {   void a::bar() {     std::cout << "bar" << std::endl;   } } 

as can see, namespaces rather add thing put in front of method name, rather remove one.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -