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* > * 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
#includethe compile replaces the statement with the entire contents of the header file, just like a simple copy paste.In your code
cuserlist()is anInline function, Incidentally,cuserlist()happens to be the class constructor in this case. So it is a constructor function which isinline. Constructors are called whenever a object gets created. For example:Both statements will call
cuserlist()which in turn then callsinit().In case of
Inline Functonsthe definition of the function is at the same time when it is declared.