473,320 Members | 2,088 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,320 software developers and data experts.

question about copy constructor in auto_ptr

Hi All
Today , I read the source code of auto_ptr. I have a question
about the copy constructor of auto_ptr.
template<class _Ty>
class auto_ptr
{
..................
}
there is a copy constructor below

template <class _Ty1>
auto_ptr(auto_ptr<_Ty1>& __a) : _M_ptr(__a.release()) {}
What is the template<class _Ty1mean? What is the different between
the type Ty and type _Ty1? Can a smart pointer of type _Ty1 be
reassigned to a smart pointer of type Ty?
Dec 1 '07 #1
2 1683
dolphin wrote:
Hi All
Today , I read the source code of auto_ptr. I have a question
about the copy constructor of auto_ptr.
template<class _Ty>
class auto_ptr
{
.................
}
there is a copy constructor below

template <class _Ty1>
auto_ptr(auto_ptr<_Ty1>& __a) : _M_ptr(__a.release()) {}
What is the template<class _Ty1mean? What is the different between
the type Ty and type _Ty1? Can a smart pointer of type _Ty1 be
reassigned to a smart pointer of type Ty?
IIF _Ty is convertible to _Ty1, say Derived to Base

#include <memory>

struct Base {};
struct Derived : Base {};

int main() {
auto_ptr<DerivedspD(new Derived);
auto_ptr<BasespB(spD);
}
Dec 1 '07 #2
On 12ÔÂ1ÈÕ, ÏÂÎç9ʱ27·Ö, dolphin <jdxyw2...@gmail.comwrote:
Hi All
Today , I read the source code of auto_ptr. I have a question
about the copy constructor of auto_ptr.
template<class _Ty>
class auto_ptr
{
.................}

there is a copy constructor below

template <class _Ty1>
auto_ptr(auto_ptr<_Ty1>& __a) : _M_ptr(__a.release()) {}
What is the template<class _Ty1mean? What is the different between
the type Ty and type _Ty1? Can a smart pointer of type _Ty1 be
reassigned to a smart pointer of type Ty?
The template<class _Ty1means another type than auto_ptr<Ty>.
It's not a copy constructor, but a normal constructor with one
parameter.
The auto_ptr<_Ty1can be reassigned to auto_ptr<_Ty>, only when _Ty1
can be reassigned to _Ty. Such as, when _Ty1 is equal to _Ty, or _Ty1
is the derived class of _Ty.
Dec 1 '07 #3

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

Similar topics

10
by: Lloyd Dupont | last post by:
how do I redefine the new operator? for all the structure I use at once! (some of them comes from C include files). The rationale: I'm writting a managed C++ wrapper around C API (external...
3
by: puzzlecracker | last post by:
Given, class X { //..... private: auto_ptr<Y> y; };
14
by: kathy | last post by:
I have a vector which I save the pointer. function() { std::vector <CMyData*> vpMyData; vpMyData.push_back(new CMyData(0)); vpMyData.push_back(new CMyData(1)); vpMyData.push_back(new...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
2
by: flopbucket | last post by:
I saw the following post a few messages back and am trying to understand exactly why this is so. Thanks for the information. My comments begin with **. The original post was arguing that STL...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
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: 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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.