Article View: comp.std.c++
Article #3947Re: Must exception classes have copy constructors?
From: Jason D. Morris
Date: Sat, 06 Apr 1996 19:33
Date: Sat, 06 Apr 1996 19:33
62 lines
2783 bytes
2783 bytes
On 05 Apr 96 02:58:28 GMT, Alan Griffiths <aGriffiths@ma.ccngroup.com>
wrote:
>I've just tried porting some code to MSVC4 (it compiles and works with
>SC7.1 and BC4.5).
>
>In effect it appears that the Microsoft compiler _requires_ anything
>thrown as an exception to have an accessible copy constructor. (I've
>tended to make copy constructors private to ensure catch-by-reference
>and aviod slicing bugs.)
>
>Looking through the DWP I can't see any reason to require a copy
>constructor, but there are references to copying temporaries.
>
>Is MSVC4 correct?
>
>code:
>class MyBaseException {
>public: MyBaseException() {}
> virtual char* what() = 0;
>private: MyBaseException(const MyBaseException&);
>};
>
>class MyException1 : public MyBaseException {
>public: virtual char* what() { return "MyException1"; }
>};
>
>int main() {
> try {
> throw MyException1();
>// error C2700: 'class MyException1' : cannot be thrown (use -W4 for more info)
>
>// warning C4671: 'MyException1' : the copy constructor is inaccessible
> } catch (MyBaseException& e) {
> cout << "OK - got here (" << __LINE__ << ") : have a " << e.what()
> << endl;
> } catch (...) {
> cout << "ERROR - got here (" << __LINE__ << ")" << endl;
> }
> return 0;
>}
>
Here's what BC++ 5.0 generates...
Borland C++ 5.0 for Win32 Copyright (c) 1993, 1996 Borland
International
MS-EXCPT.CPP:
Error MS-EXCPT.CPP 15: Compiler could not generate copy constructor
for class 'MyException1' in function main()
*** 1 errors in Compile ***
It appears we have an agreement in interpretation of the DWP.
Jason
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<3165fcc3.2177561@nntp.ix.netcom.com>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernReferences:
<606373375wnr@ma.ccngroup.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: jdmorris@ix.netcom.com (Jason D. Morris) Newsgroups: comp.std.c++ Subject: Re: Must exception classes have copy constructors? Date: 06 Apr 1996 11:33:58 PST Organization: Netcom Approved: austern@isolde.mti.sgi.com Message-ID: <3165fcc3.2177561@nntp.ix.netcom.com> References: <606373375wnr@ma.ccngroup.com> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: Sat, 06 Apr 1996 05:11:45 GMT X-NETCOM-Date: Fri Apr 05 9:10:50 PM PST 1996 X-Newsreader: Forte Agent .99d/32.182 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMWbHJ0y4NqrwXLNJAQFLdAIAzXl2DYLHV/c36+Ty7qCRDbYFjEyC9WSl S3zQTwMI1jk8Eoz2tfmTvKGLmX+QCCaBBg0lSogV5Zh617nRg14VLA== =+omO Originator: austern@isolde.mti.sgi.com