473,397 Members | 1,974 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,397 software developers and data experts.

what does this mean? const _Ty *_Myptr

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
7 9752
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

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

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
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

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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
9
by: datastructure | last post by:
Copyright (c) 2003 by James J. Perry. All Rights Reserved. char Square::validList = {'r', 'g', 'b'}; //missing an element, is 0 int Square::numValues = 3; Square::Square() { value =...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
6
by: G Patel | last post by:
I've been looking at some code for string functions (certain implementation of them) and the non modified string is usually declared as a const char *s in the parameter list. I was wondering,...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
7
by: rsk | last post by:
char *i_reg_fname = "none"; -- Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/ More information at http://www.talkaboutprogramming.com/faq.html
5
by: brettcclark | last post by:
This define is from the Vista SDK from propidl.h: #ifdef __cplusplus #define REFPROPVARIANT const PROPVARIANT & #else #define REFPROPVARIANT const PROPVARIANT * __MIDL_CONST #endif
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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 projectplanning, coding, testing,...
0
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...

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.