static variables

This is a discussion on static variables within the C++ Programming forums, part of the General Programming Boards category; I have this statement inside a function, that opens a XML file. Code: static auto_ptr<Remote> remoteXML (Remote_("../config/remote_conf.xml")); The XML file ...

  1. #1
    frs
    frs is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    29

    static variables

    I have this statement inside a function, that opens a XML file.

    Code:
    static auto_ptr<Remote> remoteXML (Remote_("../config/remote_conf.xml"));
    The XML file is opened every-time I call the function or is opened only in the first call??

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I believe that it will be open every time.

    static, in this case, will just make this function invisible to other program modules (e.g. files).

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    No, static variables inside functions don't make the functions static itself. This will be opened only in the first call.
    And I wouldn't make it this way, since you will not be able to use this variable outside the function. It looks like the function does not open an XML file, but only creates an instance of object. You should consider returning a shared_ptr<Remote> so you will be able to open more than one file at a time.
    Last edited by kmdv; 09-05-2010 at 10:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 03:16 AM
  2. Static variables
    By ashughs in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2006, 10:21 AM
  3. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 07:35 AM
  4. Visual C++ 6/.net Debug static variables
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 04:45 AM
  5. Replies: 5
    Last Post: 11-19-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21