c++ - Forward Declaration of Class, Function -


when forward declarations of functions work in source file (.cpp), why same doesn't work classes ?

thanks.

// main.cpp  void forwarddeclaredfunction() ; // correct   class 1 ; // why wrong   int one:: statvar = 10 ;  void 1 :: anyaccess() {   std::cout << "\n statvar:\t " << statvar ;  std::cout << "\n classvar:\t" << classvar ; }  class 1 {   public:   void anyaccess() ;   static int statvar ;   private:   int  classvar ;  } ;   int main (int argc, char * const argv[]) {   1 *obj = new 1 ;          return 0; }  void forwarddeclaredfunction() { } 

forward declaration can work classes too:

class foo;  class bar { public:     foo *myfoo; // has pointer, catching this! };  class foo { public:     int value; }; 

the above code shows forward declaration of foo class, using variable of type foo* in class (bar), actual definition of foo class. c++ doesn't care if leave things unimplemented long implement them before using code. defining pointers objects of type not "using code."

quick, dirty reply hope helps.

edit: declaring non-pointer variable of class thats unimplemented not compile replies stated out. doing meant "using code." in case, foo constructor called whenever bar constructor called, given has member variable of type foo. since compiler doesn't know plan on implementing foo later on, throw error. sorry mistake ;).


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -