Newsgroup: comp.lang.c++
Subject: Where is RAII when you need it?
From: ram@...
Date: 17 Nov 2013 19:19:07 GMT
Today, I wanted to show RAII, like this:
#include <iostream>
#include <ostream>
#include <stdexcept>
struct K
{ K (){ ::std::cerr << "K" << ::std::endl; }
~K(){ ::std::cerr << "~K" << ::std::endl; }};
struct R
{ K * k; R() : k{ new K{} }{}
~R() { ::std::cerr << "~R" << ::std::endl; delete k; }};
int main(){ R r{}; throw ::std::runtime_error( "error" ); }
The output was:
K
terminate called after throwing an instance of 'std::runtime_error'
what(): error
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
--------------------------------
Process exited with return value 3
Press any key to continue . . .
No ~K nor ~R in sight! Why?
Subject: Where is RAII when you need it?
From: ram@...
Date: 17 Nov 2013 19:19:07 GMT
Today, I wanted to show RAII, like this:
#include <iostream>
#include <ostream>
#include <stdexcept>
struct K
{ K (){ ::std::cerr << "K" << ::std::endl; }
~K(){ ::std::cerr << "~K" << ::std::endl; }};
struct R
{ K * k; R() : k{ new K{} }{}
~R() { ::std::cerr << "~R" << ::std::endl; delete k; }};
int main(){ R r{}; throw ::std::runtime_error( "error" ); }
The output was:
K
terminate called after throwing an instance of 'std::runtime_error'
what(): error
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
--------------------------------
Process exited with return value 3
Press any key to continue . . .
No ~K nor ~R in sight! Why?
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?130533-Where-is-RAII-when-you-need-it&goto=newpost
View all the progranning help forums at:
http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums
No comments:
Post a Comment