By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,991 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,991 IT Pros & Developers. It's quick & easy.

what does this mean? const _Ty *_Myptr

P: n/a
Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
....
private:
const _Ty *_Myptr;
}

what is the meaning of const _Ty *_Myptr; in a class member
decalration?

I appreicate your kind help!

Shuisheng

Dec 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Dec 16, 11:51 am, "shuisheng" <shuishen...@yahoo.comwrote:
Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
...
private:
const _Ty *_Myptr;

}what is the meaning of const _Ty *_Myptr; in a class member
decalration?

I appreicate your kind help!

Shuisheng
http://www.parashift.com/c++-faq-lite/templates.html

Dec 16 '06 #2

P: n/a

shuisheng wrote:
Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
...
private:
const _Ty *_Myptr;
}

what is the meaning of const _Ty *_Myptr; in a class member
decalration?
The class above has a template parameter <class _Ty>, substitute that
parameter with whatever type you are passing to the template and you'll
get your answer. If you specify < int as auto_ptr's template
parameter:
std::auto_ptr< int ap_n(new int);
.... then the member will be:
const int* _Myptr;

Its a very, very simple construct. No voodoo involved.
You can confirm such doubts by simply writing something like this:

#include <iostream>
#include <ostream>
#include <typeinfo>

template< typename T >
struct Test
{
T t;
};

int main()
{
Test< double dtest;
std::cout << typeid(dtest.t).name() << std::endl;
Test< char ctest;
std::cout << typeid(ctest.t).name() << std::endl;
}

Although, what type T will be is obvious.

Dec 16 '06 #3

P: n/a

shuisheng wrote:
Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
...
private:
const _Ty *_Myptr;
}

what is the meaning of const _Ty *_Myptr; in a class member
decalration?

I appreicate your kind help!

Shuisheng
To make my question more clear. I am thinking that the 'const'
qualifier should not be here because in auto_ptr, such as reset, its
content is changed. Where is my wrong?

Thanks.

Dec 16 '06 #4

P: n/a
shuisheng wrote:
>
shuisheng wrote:
>Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
...
private:
const _Ty *_Myptr;
}

what is the meaning of const _Ty *_Myptr; in a class member
decalration?

I appreicate your kind help!

Shuisheng

To make my question more clear. I am thinking that the 'const'
qualifier should not be here because in auto_ptr, such as reset, its
content is changed. Where is my wrong?
Distinguish the types

T const *

and

T * const

Best

Kai-Uwe Bux
Dec 16 '06 #5

P: n/a

shuisheng wrote:
shuisheng wrote:
Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
...
private:
const _Ty *_Myptr;
}

what is the meaning of const _Ty *_Myptr; in a class member
decalration?

I appreicate your kind help!

Shuisheng

To make my question more clear. I am thinking that the 'const'
qualifier should not be here because in auto_ptr, such as reset, its
content is changed. Where is my wrong?

Thanks.
Its not a constant pointer, its a mutable pointer to a constant
variable (see Kai-Uwe's Post).
Its contents are not changed during a reset(). Its given ownership of
an entirely new object (which deletes the old object, if any).
Reseating the pointer is not prohibited.

#include <iostream>
#include <ostream>

int main()
{
int n(5);
const int* p_n(&n);
// *p_n = 6; // error
std::cout << *p_n << std::endl;

int m(99);
// *p_n = 100; // error
p_n = &m; // reseating ptr , perfectly ok
std::cout << *p_n << std::endl;
}

/*
5
99
*/

Dec 17 '06 #6

P: n/a
Salt_Peter wrote:
shuisheng wrote:
>shuisheng wrote:
>>Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
...
private:
const _Ty *_Myptr;
}

what is the meaning of const _Ty *_Myptr; in a class member
decalration?

I appreicate your kind help!

Shuisheng

To make my question more clear. I am thinking that the 'const'
qualifier should not be here because in auto_ptr, such as reset,
its content is changed. Where is my wrong?

Thanks.

Its not a constant pointer, its a mutable pointer to a constant
variable (see Kai-Uwe's Post).
Its contents are not changed during a reset(). Its given ownership
of an entirely new object (which deletes the old object, if any).
Reseating the pointer is not prohibited.
It is kind of interesting though, that an auto_ptr implementation stores a
pointer-to-const. We are still able to assign to the underlying element
using auto_ptr::operator*().

An implementation detail, I know, but still surprising.
Bo Persson
Dec 17 '06 #7

P: n/a

Bo Persson wrote:
Salt_Peter wrote:
shuisheng wrote:
shuisheng wrote:
Dear All,

I find in std::auto_ptr, the private member is define as

template<class _Ty>
class auto_ptr
{
...
private:
const _Ty *_Myptr;
}

what is the meaning of const _Ty *_Myptr; in a class member
decalration?

I appreicate your kind help!

Shuisheng

To make my question more clear. I am thinking that the 'const'
qualifier should not be here because in auto_ptr, such as reset,
its content is changed. Where is my wrong?

Thanks.
Its not a constant pointer, its a mutable pointer to a constant
variable (see Kai-Uwe's Post).
Its contents are not changed during a reset(). Its given ownership
of an entirely new object (which deletes the old object, if any).
Reseating the pointer is not prohibited.

It is kind of interesting though, that an auto_ptr implementation stores a
pointer-to-const. We are still able to assign to the underlying element
using auto_ptr::operator*().

An implementation detail, I know, but still surprising.
Ok, but most don't store a ptr_to_constant. What is really strange with
auto_ptrs is that both the copy ctor and the assignment operator modify
a non-constant rvalue which renders the resulting rvalue into a nullptr
(transfer of ownership).

template< class T >
class auto_ptr {
...
autoptr( auto_ptr< T >& copy ) { ... } // non-const copy is modified
...
};

And a reference can result in a transfer of ownership:

#include <iostream>
#include <ostream>
#include <memory>

void foo(std::auto_ptr< int >& r_ap)
{
std::auto_ptr< int p_local(r_ap);
std::cout << *r_ap << std::endl; // error !!!
}

int main()
{
std::auto_ptr< int p_n(new int(5));
foo(p_n);
std::cout << *p_n << std::endl; // error !!!
}

Fortunately, a const ref to auto_ptr<Tsolves that issue.

With respect of its shortcomings (ie: not copyable and can't support
arrays) its a far, far better alternative than new/delete where
appropriate.

Dec 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.