Changing a static member in a const member function in C ++

I am working on a linked list, but I cannot change the value of the current pointer in the const function "void Print () const"

In the Print function I want to make "current = head" and then increase it as "current = current-> link" , but cannot do this, bcz it shows

"error C3490:" current "cannot be changed because it is accessed through the const e: \ Cpp \ projects \ data structure ass-1 \ data structure ass-1 \ source.cpp 83 1 Data Structure Ass-1"

#include<iostream> struct node { int data; node *link; }; class List { node *head,*current,*last; public: List(); // List(const List&); // ~List(); void print() const; }; using namespace std; int main() { List List1; } void List::print() const { current=head; //here is my error current=current->link; } List::List():current(head) { } 
+4
source share
6 answers

Declare current as a local variable to print the method. If you use current as a member variable for other purposes, the local variable will obscure it. If you do not use current as a member variable, you can simply delete it.

0
source

If a member function of a class is declared as const :

 void print() const; 

this means that this function cannot change the data elements of its class. In your case, the variables are:

 node *head,*current,*last; 

cannot be changed in print() body. Therefore, you cannot change the addresses that these pointers point to. The way to solve this problem is to define the local variable temp in your print() function. Such a variable can be changed and do the same work as current :

 void List::print() const { node *temp; temp=head; temp=temp->link; } 
+4
source

When you declare a member function const , the this pointer becomes const inside the const function when it is called on the object.

The value of the const member function prevents direct or immediate modification of the data members of the class.

Direct implies what you do in your program (changing the data members directly in the const member function, which violates its purpose). It is perfectly normal to perform any operations with data members if you do not modify them. In addition, you can call other const member functions inside the const member function.

And indirect means that you cannot even call other member functions of a non-const class, because they can modify data elements.

Usually const member functions are used when you just want to get / read values. Therefore, in your case, you should not use the const member function.

Alternatively, you can call non-const and const member functions on a non-const object.

+3
source

You have declared the print () function as const. This means that the function does not change the member variables of the class, but this is the first thing you do in the definition of the function.

+2
source

Change node *head,*current,*last; on mutable node *head,*current,*last;

+1
source

The error tells you what exactly is happening - when you say List::print() const , you promise not to change any of the members of your list. But then you go and try to change current .

It's hard to say without seeing the rest of your code, but perhaps current should not be a member variable and should be local to List::print() . Or perhaps List::print() should not be const. You can also make current volatile, but this is almost always bad practice.

0
source

Source: https://habr.com/ru/post/1499988/


All Articles