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. Copy constructor in Exception Class

Copy constructor in Exception Class

Scheduled Pinned Locked Moved C / C++ / MFC
2 Posts 2 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.
  • R Offline
    R Offline
    Robin
    wrote on last edited by
    #1

    I have a exception class which I would want to be catch by ref only, so I declare the copy constructor private. However, VC++ 6 refuses to compile, giving me an error an error that the exception object cannot be thrown because of an inaccessible copy constructor. But upon checking, the copy constructor is only called when the exception object is caught by value eg catch(MyExceptionClass e) // only this needs the copy constructor {} catch(MyExceptionClass &e) // this doesn't need the copy constructor {} So has anyone been able to ensure the exception object is to be catch by ref only, not value??

    T 1 Reply Last reply
    0
    • R Robin

      I have a exception class which I would want to be catch by ref only, so I declare the copy constructor private. However, VC++ 6 refuses to compile, giving me an error an error that the exception object cannot be thrown because of an inaccessible copy constructor. But upon checking, the copy constructor is only called when the exception object is caught by value eg catch(MyExceptionClass e) // only this needs the copy constructor {} catch(MyExceptionClass &e) // this doesn't need the copy constructor {} So has anyone been able to ensure the exception object is to be catch by ref only, not value??

      T Offline
      T Offline
      Tim Deveaux
      wrote on last edited by
      #2

      My tests (VC6 and Borland CPPB4) disagree with yours - the copy constructor is called for the catch declared as taking a reference. For a function that takes a reference, it's true that the CC isn't called - the trick here is that the throw() operand causes a copy of its argument to be created. Check out "C++ Exception Examples" in the MSDN.

      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