Question about const parameter and pointer
Oct 7, 2016 at 9:07pm UTC
This is a simple project that I'm working with. This is a linked list that supports a number of operations similar to std::list.
I was writing one of the insert functions to allow users to insert an element at a position (iterator).
Please pay attention to the insert function in the code.
I declared itr as const reference. However, the compiler seems to allow me to perform the following even though these two operations essentially "change" the const parameter. Why is that?
1 2
itr.current->prev = temp;
itr.current->prev = toAdd;
Thank you in advance for the assistance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
#include <iostream>
#include <algorithm>
#include <initializer_list>
/*
Create a simple doubly LinkedList class
Implement const_iterator and iterator interfaces to allow list traversal
*/
class LinkedList
{
private :
// LinkedList Node
struct Node
{
int data = 0;
Node* next = nullptr ;
Node* prev = nullptr ;
Node( int data ): next{ nullptr } , prev{ nullptr } , data{ data }
{ }
};
public :
// const_iterator interface to allow traversal
class const_iterator
{
protected :
Node* current;
// protected constructor that accepts Node*
// It is protected to prevent user from calling it explicitly
const_iterator( Node* c ): current{ c }
{ }
int & retrieve() const
{ return current->data; }
// LinkedList class needs to be able to access protected constructor
friend class LinkedList;
public :
// Default Constructor
const_iterator(): current{ nullptr }
{ }
// Returns element pointed by current
const int & operator * () const
{ return retrieve(); }
// Pre-increment operator
const_iterator & operator ++ ()
{
if ( current != nullptr ) {
current = current->next;
return *this ;
}
}
// Post-increment operator
const_iterator operator ++ ( int )
{
const_iterator old = *this ;
++( *this );
return old;
}
// Pre-decrement operator
const_iterator & operator -- ()
{
if ( current != nullptr ) {
current = current->prev;
return *this ;
}
}
// Post-decrement operator
const_iterator operator -- ( int )
{
const_iterator old = *this ;
--( *this );
return old;
}
// Compare two const_iterator objects
bool operator == ( const const_iterator &rhs ) const
{ return current == rhs.current; }
// Compare two const_iterator objects
bool operator != ( const const_iterator &rhs ) const
{ return current != rhs.current; }
};
// iterator interface
class iterator: public const_iterator
{
public :
// Default Constructor
iterator() {}
// Allow read/write access to the data
int & operator * ()
{ return const_iterator::retrieve(); }
// Pre-increment operator
iterator & operator ++ ()
{
if ( current != nullptr ) {
current = current->next;
return *this ;
}
}
// Post-increment operator
iterator operator ++ ( int )
{
iterator old = *this ;
++( *this );
return old;
}
private :
// Prevent user from calling this constructor explicitly
iterator( Node* c ): const_iterator{ c }
{ }
// Allow LinkedList class to access private constructor
friend class LinkedList;
};
public :
// Default Constructor
LinkedList() {}
// Copy Constructor
LinkedList( const LinkedList &rhs )
{
Node* temp = rhs.head;
while ( temp != nullptr ) {
push_back( temp->data );
temp = temp->next;
}
}
// Brace-Enclosed Initializer
LinkedList( std::initializer_list<int > iList )
{
for ( int x : iList ) {
this ->push_back( x );
}
}
// Assignment Operator
LinkedList & operator = ( const LinkedList &rhs )
{
LinkedList copy = rhs;
std::swap( *this , copy );
return *this ;
}
// Destructor
~LinkedList()
{
while ( head != nullptr ) {
Node* temp = head;
head = head->next;
delete temp;
}
}
/**
* Insert new element before the given itr position
* This container must already have at least 1 element.
* If not, nothing will happen
*/
iterator insert( const iterator &itr , const int &x )
{
if ( itr.current != nullptr ) {
Node* temp = itr.current->prev;
// current->prev is nullptr, which means, current is the head
if ( temp == nullptr ) {
// Allocate memory for temp
temp = new Node{ x };
// Set new Node as the head
head = temp;
// Set new Node's next to current Node
temp->next = itr.current;
// Set current node's prev to the new Node
itr.current->prev = temp;
// Increment size
theSize++;
}
// current->prev is not the head.
else {
Node* toAdd = new Node{ x };
// temp is pointing at current->prev. Set next to the new Node
temp->next = toAdd;
// Set new Node's prev to temp
toAdd->prev = temp;
// Set new Node's next to current itr's Node
toAdd->next = itr.current;
// Set current Node's prev to the new Node
itr.current->prev = toAdd;
// Increment size
theSize++;
}
}
return itr;
}
void push_front( int x )
{
if ( head == nullptr ) {
head = new Node{ x };
tail = head;
theSize++;
}
else {
Node* temp = head;
head = new Node{ x };
head->next = temp;
temp->prev = head;
theSize++;
}
}
void push_back( int x )
{
if ( tail == nullptr ) {
tail = new Node{ x };
head = tail;
theSize++;
}
else {
Node* temp = tail;
tail = new Node{ x };
temp->next = tail;
tail->prev = temp;
theSize++;
}
}
void pop_front()
{
if ( head != nullptr ) {
Node* temp = head;
head = temp->next;
head->prev = nullptr ;
delete temp;
theSize--;
}
}
void pop_back()
{
if ( tail != nullptr ) {
Node* temp = tail;
tail = temp->prev;
tail->next = nullptr ;
delete temp;
theSize--;
}
}
int size()
{ return theSize; }
const_iterator begin() const
{ return const_iterator{ head }; }
const_iterator end() const
{ return const_iterator{ tail->next }; }
iterator begin()
{ return iterator{ head }; }
iterator end()
{ return iterator{ tail->next }; }
private :
Node *head = nullptr ;
Node *tail = nullptr ;
int theSize = 0;
};
// Test Driver to test operations of LinkedList
int main()
{
LinkedList list = { 1 };
LinkedList::iterator current = list.begin();
current = list.insert( current , 100 );
for ( int x : list ) {
std::cout << x << std::endl;
}
current = list.insert( current , 110 );
std::cout << std::endl;
for ( int x : list ) {
std::cout << x << std::endl;
}
std::cout << std::endl << list.size();
}
Last edited on Oct 7, 2016 at 9:10pm UTC
Oct 7, 2016 at 9:27pm UTC
I declared itr as const reference. However, the compiler seems to allow me to perform the following even though these two operations essentially "change" the const parameter. Why is that?
1 2
itr.current->prev = temp;
itr.current->prev = toAdd;
The itr is a const iterator.
The itr.current is thus a pointer to Node and therefore const.
The itr.current has the address of a Node object. That object does not need to be a const.
The assignment does change the Node object, but it does not change the address stored in the itr.current.
Oct 8, 2016 at 7:20am UTC
@keskiverto
Thank you!
Topic archived. No new replies allowed.