Unable to understand this concept of handlers in c++ -


i going through piece of code when came across new. tried write own code better understanding.

#include<iostream>  using namespace std;  class material { public: material() {     cout<<"material() called"<<endl; }  bool test_func() {     cout<<"hello world"<<endl;      return true; } };  class server { private: material *mat;  public: server() {     cout<<"server() called"<<endl; } material *matrl() {     return mat; } };  class handler { public: handler() {     cout<<"handler() called"<<endl; }  server svr;  bool demo() {     bool ret;     ret=svr.matrl()->test_func();      return ret; } };  int main() { handler h; cout<<"returned demo():"<<h.demo()<<endl;  return 0; } 

even getting desired output, is:

server() called handler() called hello world returned demo():1 

but not able understand concept here :

material *matrl() {     return mat; } 

and functionn call

ret=svr.matrl()->test_func(); 

how working , concept concept behind ? can me this???

you can avoid confusion if rewrite

material *matrl() {     return mat; } 

to

material* matrl() {     return mat; } 

both same. function returning pointer object of material type

now

ret=svr.matrl()->test_func(); 

since matr1() returns pointer object need use -> member function.or
*(svr.matr1()).test_func();


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -