473,382 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Assignment Operator with auto_ptr

Hello All,
Can somebody please explain why the following is not supported ?

class A{};

A* a = new A();

auto_ptr <A> aptr = *a;

// though this is supported.

auto_ptr <A> aptr(a);

Thanks in advance
CID

Jul 23 '05 #1
3 3179
Chumm...@gmail.com wrote:
Hello All,
Can somebody please explain why the following is not supported ?

class A{};

A* a = new A();

auto_ptr <A> aptr = *a;
Contrary to what the thread subject says, this invokes auto_ptr's
constructor. You're trying to pass the A object itself, not a pointer
to it. This is the correct way to do it:
// though this is supported.

auto_ptr <A> aptr(a);


If you want to use the equals sign syntax, this line would be:
auto_ptr<A> aptr = a;

Kristo

Jul 23 '05 #2
CID

Kristo wrote:
Chumm...@gmail.com wrote:
Hello All,
Can somebody please explain why the following is not supported ?

class A{};

A* a = new A();

auto_ptr <A> aptr = *a;
Contrary to what the thread subject says, this invokes auto_ptr's
constructor. You're trying to pass the A object itself, not a

pointer to it. This is the correct way to do it:
// though this is supported.

auto_ptr <A> aptr(a);


If you want to use the equals sign syntax, this line would be:
auto_ptr<A> aptr = a;

Kristo


This will result in the following error in g++, which was my first
doubt was.

error: conversion from `A*' to non-scalar type
`std::auto_ptr<A>' requested

Thanks for the reply
CID

Jul 23 '05 #3
CID schrieb:
Kristo wrote:
Chumm...@gmail.com wrote:
Hello All,
Can somebody please explain why the following is not supported ?

class A{};

A* a = new A();

auto_ptr <A> aptr = *a;


Contrary to what the thread subject says, this invokes auto_ptr's
constructor. You're trying to pass the A object itself, not a


pointer
to it. This is the correct way to do it:

// though this is supported.

auto_ptr <A> aptr(a);


If you want to use the equals sign syntax, this line would be:
auto_ptr<A> aptr = a;

Kristo

This will result in the following error in g++, which was my first
doubt was.

error: conversion from `A*' to non-scalar type
`std::auto_ptr<A>' requested

that's because the constructor of auto_ptr is "explicit", and there's a
obvious reason for it.
use
auto_ptr<A> aptr=auto_ptr<A>(a);
instead
Jul 23 '05 #4

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

Similar topics

24
by: Marcin Vorbrodt | last post by:
Is there any reason why auto_ptr does not have the cast operator like this: operator void* (); So that one could easily test auto_ptr for NULL ??? Standard does not declare such operator. Is...
4
by: gsyoon | last post by:
hi, all. I'm trying to make a "framework" to store the return value of a function to a global memory. My first attempt was 1) void store_to_global( char * type_name ) { if ( strcmp(...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
12
by: Alan Ning | last post by:
I have a question on assignment operator. People in my company likes to make their assignment operator const. So we have const A& operator=(const A& obSrc) But everywhere online, I see
10
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
6
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the...
7
by: Scott Gifford | last post by:
As a possible solution to a problem I'm trying to solve with an iterator (see an earlier post by me with subject "Iterator implementation questions: copy constructor and postfix increment"), I'm...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.