473,796 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

auto_ptr: reassigning to member of owned object

Is it legal to reassign an auto_ptr to another auto_ptr that is a member of
the object the auto_ptr is holding before the reassignment? (Confused yet?
:-p)

#include <memory>
class Node
{
public:
Node() : next(0) {}
Node(Node* node) : next(node) {}
std::auto_ptr<N ode> next;
};

void main()
{
Node node(new Node(new Node()));
node.next = (*node.next).ne xt; // <-- is this valid?
}

As I see it, this program could cause problems if the object node.next owns
is deleted before (*node.next).ne xt is evaluated. I even think the auto_ptr
assignment operator needs to make a temporary copy of (*node.next).ne xt.
The above code compiles and runs on my system, but is it legal according to
the C++ Standard?

tia,
Joost
Jul 19 '05 #1
2 1993
Joost Ronkes Agerbeek wrote:
Is it legal to reassign an auto_ptr to another auto_ptr that is a member of
the object the auto_ptr is holding before the reassignment? (Confused yet?
:-p)

#include <memory>
class Node
{
public:
Node() : next(0) {}
Node(Node* node) : next(node) {}
std::auto_ptr<N ode> next;
};

void main()
obligatory - main must return int comment.
{
Node node(new Node(new Node()));
node.next = (*node.next).ne xt; // <-- is this valid?
}

As I see it, this program could cause problems if the object node.next owns
is deleted before (*node.next).ne xt is evaluated.
That's kind of hard since operator= can't be called until
(*node.next).ne xt is evaluated.

I even think the auto_ptr assignment operator needs to make a temporary copy of (*node.next).ne xt.
The above code compiles and runs on my system, but is it legal according to
the C++ Standard?


Jul 19 '05 #2
> > void main()
obligatory - main must return int comment.

You're right of course. My mistake.
{
Node node(new Node(new Node()));
node.next = (*node.next).ne xt; // <-- is this valid?
}

As I see it, this program could cause problems if the object node.next owns is deleted before (*node.next).ne xt is evaluated.


That's kind of hard since operator= can't be called until
(*node.next).ne xt is evaluated.


That makes sense. :-D

Problem solved then :-), thanks a bunch.
Jul 19 '05 #3

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

Similar topics

23
2663
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single ownership expectation on auto_pointer.
10
2549
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. # include <iostream> # include <memory> class CallbackBase { public: virtual void operator()() const { };
6
1522
by: Azdo | last post by:
Hello, if i wanted to access the elements of an array which is owned by an auto_ptr, as: #include <memory> int main(){ std::auto_ptr<int> p(new int); p=2; //Error: no operator defined
2
4581
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the following (in file testInteger.cc): line 38: std::auto_ptr<HInteger> d(new HInteger(5)); line 39: std::auto_ptr<HObject> e(new HObject());
10
1911
by: Diego Martins | last post by:
I just discovered a serious issue when compiling code using auto_ptr<> and VC 8.0 consider some sort of create() function blahblah * create() { return new blahblah; } and a pretty auto_ptr
39
864
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like Query("123");
5
1723
by: Soumen | last post by:
For one of my project, I want to use auto_ptr. But I'm facing few issues. 1. Say I've Controller class. I don't want to allow user to create object of this class unless all arguments are valid. So I've made the constructor private and given one static method to create an object of this class. Since one of senior members in the team doesn't like throwing exception, I've made this static method to return NULL pointer when arguments are...
9
1815
by: Soumen | last post by:
If I've class with one of data members of auto_ptr type, how should I write copy ctr or copy assignment operator if I don't intend to allow transfer of ownership happen? Declaring that member const could be one option I guess. Clarify me if this understanding is wrong. Do I need to disable these (copy ctr and assignment operator) in such cases? What happens when I don't write one and use the compiler generated one? Does transfer of...
17
10074
by: Ankur Arora | last post by:
Hi All, I'm building a sample application that uses a custom auto_ptr implementation. The program crashes with the following output:- Output (Debug Assertion failed) ----------
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.