Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. General C++ question on constructors

General C++ question on constructors

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++lounge
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    melwyn
    wrote on last edited by
    #1

    Hi, I have a general question on constructors in C++. If my constructor throws an exception is the object created? If so, is it a valid object? Can I invoke the methods of the class using that object? How should you handle this scenario? Thanks, Mel

    E M M 3 Replies Last reply
    0
    • M melwyn

      Hi, I have a general question on constructors in C++. If my constructor throws an exception is the object created? If so, is it a valid object? Can I invoke the methods of the class using that object? How should you handle this scenario? Thanks, Mel

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #2

      May be the answer can change depending on compiler, but as far as I know, the constructor is called after memory allocation has occurred and its first instructions (think as executed at the "open brace") are "call the bases constructors and call the members constructors". At that point the object can be assumed as effectively constructed and formed. The problem is another: if your object is used as a base of another object and your constructor throws an exception ... The "other" object is not properly constructed. But the memory is allocated. But the other object constructor cannot handle the exception as well, since it didn’t reach any possible "try/catch" block. (It escaped from its open brace) In fact your exception escapes the constructors leaving the instantiator (the piece of program that asked to create the outer object) with an allocated, but partially constructed object. The fact that "delete" can be properly called by the calling program, mostly depend on what constructors and destructors do. Moral: avoid to throw exceptions in constructors... a "two phase construction" (by calling a separate "init" function after the construction) can be safer and give a more predictable behaviour. 2 bugs found. > recompile ... 65534 bugs found. :doh:

      1 Reply Last reply
      0
      • M melwyn

        Hi, I have a general question on constructors in C++. If my constructor throws an exception is the object created? If so, is it a valid object? Can I invoke the methods of the class using that object? How should you handle this scenario? Thanks, Mel

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        No, the object is not created if the constructor throws an exception. See GotW #66[^] for a detailed discussion of the subject.

        1 Reply Last reply
        0
        • M melwyn

          Hi, I have a general question on constructors in C++. If my constructor throws an exception is the object created? If so, is it a valid object? Can I invoke the methods of the class using that object? How should you handle this scenario? Thanks, Mel

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          The C++ standard says that if a constructor throws an exception any memory allocated will be freed, but no destructor will be called. It is not a valid object and you cannot invoke members. If you try to do so the behaviour is undefined. In C++, undefined usually means it will crash. I'm pretty sure that VC++ implements this behaviour correctly. In essence it wraps a new operation in a try/catch block and calls ::operator delete (or the class's operator delete, if it or a base class implements one), if C++ exception handling is enabled. Stability. What an interesting concept. -- Chris Maunder

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups