Thread: Calling other objects methods inside a constructor

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    Thumbs up Calling other objects methods inside a constructor

    If I have this code

    Code:
    File 1:
    
    typedef class c_Semaphore
    {
    public :
    c_Semaphore() 
    {
         if (sem_init(&sem, 0,0)) Log.Error("Error in semaphore init (constructor)");
    }
    ~c_Semaphore()	{	sem_destroy(&sem);	}
    	sem_t  sem;	
    } Type_Semaphore;
    
    Type_Semaphore Sem;
    
    
    File 2 :
    typedef class c_Log
    {
    public :
       ...
       void Error(const char * ch) {..}
    } Type_Log;
    
    Type_Log Log;
    Would you suppose that the compiler+linker is sufficiently fine to call c_Log constructor for object Log and after the constructor for Sem or , as they are in different modules, the compiler+linker gives errors or does not garauntee the correct order in constructor callings?

    thank you
    Last edited by mynickmynick; 09-17-2008 at 02:41 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I do not think you can rely on Log being constructed before Sem.

    Log looks like something that could be implemented using a Singleton pattern, so you may not need a constructor as such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    I do not think you can rely on Log being constructed before Sem.

    Log looks like something that could be implemented using a Singleton pattern, so you may not need a constructor as such.

    --
    Mats
    What is a singleton pattern?
    Log is a cyclic buffer where to write by multiple threads, which periodically is saved to a log file or sent via socket to a client log

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    google gives this:
    http://en.wikipedia.org/wiki/Singleton_pattern
    which is a much more complete explanation than what I can give. (But in short, it is an object that may have may instances that is represented behind the scenes by a single object).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's no guaranteed order of initialization between globals of different modules.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    There's no guaranteed order of initialization between globals of different modules.
    So, presumably the two global objects could be put in a "globals.cpp" or "main.cpp" and the order of the objects themselves would define the order of initialization.

    This is of course not a very natural way to solve the problem, but it IS a possibility - the biggest problem comes when you have hundreds of different modules with various global objects, in which case wrapping the object into something that can perform it's initialization as needed is the right solution, such as the singleton pattern. [There are other solutions that do similar things, but they all rely on "if this is not set, create an object, else return the existing object"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  2. calling a member function inside a vector
    By finnepower in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2004, 02:44 AM
  3. calling constructor crashes program?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 07-04-2003, 11:17 AM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. calling methods
    By alcoholic in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2002, 11:23 AM