Alex wrote in message
<1135622352.873654.270580@g44g2000cwa.googlegroups .com>...[color=blue]
>But MyVector is just an example. I need to return a const pointer and a
>non-const pointer. And I don't want the subclasses to redefine both,
>but just one, as I explain in the first post.[/color]
You can assign a non-const to a const, but, not a const to a non-const.
[ RW == Read-Write. RO == Read-Only ]
int o(0); // RW
int const oConst = 1; // must initialize a constant // RO
int *p = &o; // int is RW, pointer is RW
int const *pConst = &oConst; // int is RO, pointer is RW
pConst = p; // ok
pConst = pConst; // ok
p = p; // ok
// p = pConst; // won't compile (without a const_cast<>(). Think first!!)
// p is pointing to memory that can be *read* or written to.
*p = 43; // should be no problem. [ o == 43 ]
pConst = p; // should not cause a problem. the int *can* be read.
// *pConst = 43; // should cause a problem. "assignment of read-only
location"
// The 'int' is constant(RO) thru pointer, not the
pointer to it.
int * const pcConst = &o; // const pointer to an RW int.
// pcConst=p; // should cause a problem. the pointer is const.
// assignment of read-only variable `pcConst'
*pcConst = 43; // should not cause a problem. [ o == 43 ]
// int const * const pccConst( &oConst ); // ok
int const * const pccConst( &o ); // 'o' is RW
// pccConst = p; // nope. the pointer is const.
// *pccConst = 43; // nope. What it points to is RO thru pointer.
If you are the slightest bit light-headed on pointers, you should go get Mr.
Steinbach's tutorial:
-----Original Message-----
From: Alf P. Steinbach
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,no.it.programmering.c++
Date: Thursday, December 08, 2005 12:04 AM
Subject: Finally, a release candidate of the "Pointers" document
(introduction to pointers)
http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf http://home.no.net/dubjai/win32cpptu...1_examples.zip
--
Bob R
POVrookie