By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

Exception & copy constructor

P: n/a
Why does this compile:

if (err != Socket::errSuccess) {
switch (err) {
case Socket::errTimeout:
ConnectionIOException cio(new
string(myTCPStream->getErrorString()), ConnectionIOException::TIMEOUT);
throw cio;

default:
ConnectionIOException cio(new
string(myTCPStream->getErrorString()), ConnectionIOException::GENERAL);
throw cio;

}
}

and this not:

if (err != Socket::errSuccess) {
switch (err) {
case Socket::errTimeout:
throw ConnectionIOException (new
string(myTCPStream->getErrorString()), ConnectionIOException::TIMEOUT);

default:
throw ConnectionIOException (new
string(myTCPStream->getErrorString()), ConnectionIOException::GENERAL);
}
}

with this error:

Connection.cpp:85: error: no matching function for call to
`ConnectionIOException::ConnectionIOException(Conn ectionIOException)'
.../exceptions/ConnectionIOException.h:19: note: candidates are:
ConnectionIOException::ConnectionIOException(Conne ctionIOException&)
Connection.cpp:85: error: in thrown expression
Connection.cpp:88: error: no matching function for call to
`ConnectionIOException::ConnectionIOException(Conn ectionIOException)'
.../exceptions/ConnectionIOException.h:19: note: candidates are:
ConnectionIOException::ConnectionIOException(Conne ctionIOException&)
Connection.cpp:88: error: in thrown expression

Thanks,

Adam

Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Adam wrote:
Why does this compile:
ConnectionIOException cio(new
string(myTCPStream->getErrorString()), ConnectionIOException::TIMEOUT); throw cio;
This copies the exception from the [non-const] object 'cio'. Thus,
a copy constructor of the form

| ConnectionIOException(ConnectionIOException&);

is sufficient.
throw ConnectionIOException (new
string(myTCPStream->getErrorString()), ConnectionIOException::TIMEOUT);

This, on the other hand, try to copy the temporary and thus requires
a copy constructor of the form

| ConnectionIOException(ConnectionIOException const&);

because temporaries cannot be bound to a non-const reference. The error
message exposes that you don't have a copy constructor taking a const
object as parameter:
../exceptions/ConnectionIOException.h:19: note: candidates are:
ConnectionIOException::ConnectionIOException(Conne ctionIOException&)

--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.