Why would the destructor be called?

This is a discussion on Why would the destructor be called? within the C++ Programming forums, part of the General Programming Boards category; In the following code: Code: iniFileParser parser = iniFileParser(fname); if(!parser.getValue("State", "width", &width)) { ... } It seems that the constructor ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    16

    Why would the destructor be called?

    In the following code:

    Code:
    iniFileParser parser = iniFileParser(fname);
    if(!parser.getValue("State", "width", &width))
    {
    ...
    }
    It seems that the constructor is being called between the time I create the parser and the time that it is used one line below. Any ideas why this might be the case?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Assuming that you are talking about an iniFileParser constructor, it would be because this constructs an iniFileParser object named parser:
    Code:
    iniFileParser parser = iniFileParser(fname);
    If you are talking about the iniFileParser destructor, then it would be because you create a temporary and copy from it (after which the copy is destroyed), and the compiler did not optimise away the extra copy in this case, perhaps because optimisations were not enabled. You should just write:
    Code:
    iniFileParser parser(fname);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Thanks...that makes sense!

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    If you are doing something with side effects in the destructor (eg, print a message), the compiler can't optimize it out, otherwise it will be changing the behaviour of your program.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,201
    Quote Originally Posted by cyberfish View Post
    If you are doing something with side effects in the destructor (eg, print a message), the compiler can't optimize it out, otherwise it will be changing the behaviour of your program.
    Yes it can. The C++ standard explicitly allows the compiler to eliminate any object if the only way of detecting the existence of that object is by tracking calls of constructors and destructors. This specifically allows the compiler to avoid constructing or destructing objects unnecessarily. A side effect of that is that any side-effects of constructors and destructors are not guaranteed to occur.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Ah never knew that.

    Stand corrected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2009, 11:05 PM
  2. Replies: 4
    Last Post: 09-21-2008, 02:27 PM
  3. callback from exe called with system()
    By leonv in forum C Programming
    Replies: 3
    Last Post: 01-25-2008, 03:12 PM
  4. AAARG!!! mental block, what is this character called: ')'
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-29-2002, 09:29 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 08:05 PM

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