0

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.

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
  • 5
    If you are new to C++, please [pick up a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). A good introductory C++ book will cover this and much more. – In silico Jun 14 '11 at 04:51

2 Answers2

1

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

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.

littleadv
  • 20,100
  • 2
  • 36
  • 50