c++ - Friend Class or Friend member function - Forward declaration and Header inclusion -


yeah question topic has been discussed many times. , i'm clear difference. i've 1 doubt related example in book.

this question related my previous question, presented 2 classes taken example in book c++ primer.

in reference classes, book quotes following paragraph, specially related declaration of member function of windowmanager class friend function. here's says:

making member function friend requires careful structuring of our programs accommodate interdependencies among declarations , definitions. in example, must order our program follows:

  • first, define window_mgr class, declares, cannot define, clear. screen must declared before clear can use members of screen.
  • next, define class screen, including friend declaration clear.
  • finally, define clear, can refer members in screen.

the code presented in question follows structure only. seems not working out. makes me think if above points misguiding or didn't implement correctly.

the problem is, when declare clear function friend function in screencls class, fall cyclic inclusion of header files. i'll brief out specific part of both classes here again:

screencls.h:

#ifndef screencls_h #define screencls_h  #include <iostream>  #include "windowmanager.h"  using namespace std;  class screencls {     friend void windowmanager::clear(screenindex);      // other code } 

here i've include windowmanager.h header file, clear function using screenindex defined there. forward declaration won't work here (correct me if i'm wrong).

now, next move on windowmanager.h:

#ifndef windowmanager_h #define windowmanager_h  #include <iostream> #include <vector> #include "screencls.h"  using namespace std;  class windowmanager { public:     // location id each screen on window     using screenindex = vector<screencls>::size_type;  private:     vector<screencls> screens{ screencls(24, 80, ' ') }; }; 

and concentrate onto declaration of screens here. have used list initializer add default screencls vector. so, here again need include windowmanager.h. , cyclic inclusion. prevents project building.

however, if change friend function declaration make entire class friend, can work forward declaring windowmanager class. in case, work fine.

so, friend function isn't working here, friend class working. so, above points not going implementation, or there's wrong classes? want know understand concept of header inclusion , forward declaration.

the questions linked in previous question describe well. it's it's not working in above situation, i'm asking again.

i guess problem screen initializer. cannot initialize data in *.h files inside class. so, suggest that:

#ifndef windowmanager_h #define windowmanager_h  #include <iostream> #include <vector> //#include "screencls.h"  using namespace std; class screencls;  class windowmanager { public:     // location id each screen on window     using screenindex = vector<screencls>::size_type;  private:     vector<screencls> screens; //{ screencls(24, 80, ' ') };    remove }; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -