const void * ptr2 ( new A(20) );
|
|
--Constructor with ID=20 ID of ptr2 is 20 ID of ptr2 is 21 after changed! -~Destructor with ID=21 End of main() Process returned 0 (0x0) execution time : 0.002 s Press ENTER to continue. |
const void *
?
const void *
:const void *
and void const*
both mean that the pointer is constant (you cannot change the address to which it is pointing)void * const
or both (const void* const
)
|
|
void* const
is a "const pointer to a void"const void*
is a "pointer to a void const"const void * const
is a "const pointer to a void const"What is the significance of "const" in const void * ? |
|
|
const
refers to the identifier to its left, the exception being that the first identifier can have const
to its left.
|
|
const void * const ptr2 ( new A(20) );
|
|
--Constructor with ID=20 ID of ptr2 is 20 ID of ptr2 is 21 after changed ! -~Destructor with ID=21 End of main() Process returned 0 (0x0) execution time : 0.002 s Press ENTER to continue. |
void *
and const void *
?gr_vector_void_star
and gr_vector_const_void_star
|
|
const
pointer.const
pointer in here is ptr2. However, on line 30, you "cast away" the const
-ness of it when you initialize x.
|
|
static_cast
etc. instead, since static_cast
will catch you if you try to do anything funny (like circumvent the const
-ness of an object):
|
|
const
-ness of an object, you can use const_cast
:
|
|
const_cast
, you might want to rethink what you're actually trying to accomplish.
const
-ness, but what I want is to automatically (not explicitly) remains the const
-ness.
|
|
gr_vector_const_void_star
won't be accidently modified? Remember the purpose of const
is to make sure it will not be modified accidently. This is the purpose of having gr_vector_const_void_star
even though we already have gr_vector_void_star
.
|
|
const
-ness of ptr2)
|
|
const
-ness of ptr2, that's why we defined ptr2 as const void *
rather than void *
const
-ness of ptr2 cannot be copied to y automatically (not explicitly), then it makes no difference whether we define ptr2 as either const void *
or void *
.
|
|
|
|
void *
or const void *
.
|
|
const
-ness of ptr2 by declaring y as a A*
instead of a const A*
.static_cast
instead of C-style casts (which is what you see above), since static_cast
will prevent you from casting away the const
-ness like that:
|
|
|
|
const
, but I want you to 'un-const
' it for me anyways."static_cast
instead.
Now I need to define ptr1 and ptr2 as void pointers. Whether the data it points to is "const" or not, this must be declared during the declaration of the pointers. How to accomplish this ? |
|
|
void
data cannot be accessed (without cast), so const void
has no further significance.const A* ptr2(new A(20));
Well, void data cannot be accessed (without cast), so const void has no further significance. |
|
|