473,508 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::auto_ptr_ref workings

Hi,
The std::auto_ptr 14.4.2 in Stroustrup ,the book talks about
"std::auto_pt_ref is to implement destructive copy semantics" , after
some more search I found that auto_ptr can be returned from function
by value,
and the class auto_ptr_ref is used to achieve that., beyond that the
workings are not mentioned. Can someone help me understand, why is an
auto_ptr_ref needed to return auto_ptr from a function, why is it
different from a normal return statement.

Thx
Digz

Apr 6 '07 #1
1 2733
digz napsal:
Hi,
The std::auto_ptr 14.4.2 in Stroustrup ,the book talks about
"std::auto_pt_ref is to implement destructive copy semantics" , after
some more search I found that auto_ptr can be returned from function
by value,
and the class auto_ptr_ref is used to achieve that., beyond that the
workings are not mentioned. Can someone help me understand, why is an
auto_ptr_ref needed to return auto_ptr from a function, why is it
different from a normal return statement.

Thx
Digz
Usage example:
class Xyz
{
public:
Xyz()
: data(new int(10))
{
// Here is some other code which may throw an exception
throw SomeException();

// Throwing exception here leads to memory leak -
// data is not cleaned
}

private:
int* data;
};

Version with std::auto_ptr:
class Xyz
{
public:
Xyz()
: data(std::auto_ptr(new int(10)))
{
// Here is some other code which may throw an exception
throw SomeException();

// Throwing exception is safe here -
// data is instance of object, so destructor will
// be called and memory will be correctly freed
}

private:
std::auto_ptr<intdata;
};

Apr 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1403
by: Alf P. Steinbach | last post by:
Code as follows: #include <iostream> // std::cout #include <string> // std::string #include <memory> // std::auto_ptr class Window { public: typedef...
14
1859
by: lutorm | last post by:
Hi, I'm having a problem with a return statement being parsed to return a function (I think). Check here: template <typename T> class A {}; template <typename T> class maker_of_A { public:...
4
3531
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
20
3711
by: Bronek Kozicki | last post by:
Hi Please try this code. I think that it's perfectly legal. However when compiled under MSVC71 stack overflow happens in first line of main, thus second line is never executed. B.
14
3570
by: Neelesh Bodas | last post by:
Hello All, My question is regarding the implementation of auto pointers - How does the class auto_ptr_ref work? eg: suppose I have : auto_ptr<int> foo(); int bar() {
2
2946
by: Markus Dehmann | last post by:
I am trying to make a pair with a string and an auto_ptr: #include <iostream> #include <map> using namespace std; int main(){ auto_ptr<intp(new int(3)); make_pair("x",p); }
9
2906
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
31
3148
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
9
2298
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.