开发者

What happen if I call functions in header file?

I am a newbie of using c++. I am following a tutorial to get some idea of basic structure of C++ and I see function calls in the header files.

I have something like this

class cuserlist : public cmutex
{
private:
  list < cuser* >_clientlist;


  bool init ();
  bool destroy ();
public:
    cuserlist ()
  {
    init ();
  }
   ~cuserlist ()
  {
    destroy ();
  }


  bool add(int cfd);
  bool remove(int cfd);
  cuser* find(int cfd);
  list < cuser* &开发者_如何学JAVAgt; * getclientlistptr() {
    return &_clientlist;
  }

};

In this header file it calls init() function in the constructor declaration. I can assume that it will call the function whenever user create cuserlist class but don't see the exact moment when it is going to be called and how it works.

Thanks in advance.


Yes, You can call functions even in header files. Doing so will call the appropriate functions. When you include header files using #include the compile replaces the statement with the entire contents of the header file, just like a simple copy paste.

In your code cuserlist() is an Inline function, Incidentally, cuserlist() happens to be the class constructor in this case. So it is a constructor function which is inline. Constructors are called whenever a object gets created. For example:

cuserlist obj;
cuserlist *ptr = new cuserlist();

Both statements will call cuserlist() which in turn then calls init().

In case of Inline Functons the definition of the function is at the same time when it is declared.


The constructor is called when the object is being initialized (either by the new operator, or directly if the object is global or on stack). Then the code in which the init function is called is being executed.

Generally header files are part of your code, during the compilation the #include directive tells the pre-processor to replace it with the content of the file stated in it, that's all. They're are not by themselves handled any differently than other portions of your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜