const Pointers in C++
By: Lakshmi in C++ Tutorials on 2007-09-14
You can use the keyword const for pointers before the type, after the type, or in both places. For example, all of the following are legal declarations:
const int * pOne; int * const pTwo; const int * const pThree;
pOne is a pointer to a constant integer. The value that is pointed to can't be changed.
pTwo is a constant pointer to an integer. The integer can be changed, but pTwo can't point to anything else.
pThree is a constant pointer to a constant integer. The value that is pointed to can't be changed, and pThree can't be changed to point to anything else.
The trick to keeping this straight is to look to the right of the keyword const to find out what is being declared constant. If the type is to the right of the keyword, it is the value that is constant. If the variable is to the right of the keyword const, it is the pointer variable itself that is constant.
const int * p1; // the int pointed to is constant int * const p2; // p2 is constant, it can't point to anything else
const Pointers and const Member Functions
When a function is declared const, the compiler flags as an error any attempt to change data in the object from within that function.
If you declare a pointer to a const object, the only methods that you can call with that pointer are const methods. Program below illustrates this.
Using pointers to const objects.
1: // 2: // Using pointers with const methods 3: 4: #include <iostream.h> 5: 6: class Rectangle 7: { 8: public: 9: Rectangle(); 10: ~Rectangle(); 11: void SetLength(int length) { itsLength = length; } 12: int GetLength() const { return itsLength; } 13: 14: void SetWidth(int width) { itsWidth = width; } 15: int GetWidth() const { return itsWidth; } 16: 17: private: 18: int itsLength; 19: int itsWidth; 20: }; 21: 22: Rectangle::Rectangle(): 23: itsWidth(5), 24: itsLength(10) 25: {} 26: 27: Rectangle::~Rectangle() 28: {} 29: 30: int main() 31: { 32: Rectangle* pRect = new Rectangle; 33: const Rectangle * pConstRect = new Rectangle; 34: Rectangle * const pConstPtr = new Rectangle; 35: 36: cout << "pRect width: " << pRect->GetWidth() << " feet\n"; 37: cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n"; 38: cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n"; 39: 40: pRect->SetWidth(10); 41: // pConstRect->SetWidth(10); 42: pConstPtr->SetWidth(10); 43: 44: cout << "pRect width: " << pRect->GetWidth() << " feet\n"; 45: cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n"; 46: cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n"; 47: return 0; 48: } Output: pRect width: 5 feet pConstRect width: 5 feet pConstPtr width: 5 feet pRect width: 10 feet pConstRect width: 5 feet pConstPtr width: 10 feet
Analysis: Lines 6-20 declare Rectangle.
Line 15 declares the GetWidth() member method const. Line 32
declares a pointer to Rectangle. Line 33 declares pConstRect,
which is a pointer to a constant Rectangle. Line 34 declares pConstPtr,
which is a constant pointer to Rectangle.
Lines 36-38 print their values.
In line 40, pRect is used to set the width of the rectangle to 10. In line 41, pConstRect would be used, but it was declared to point to a constant Rectangle. Therefore, it cannot legally call a non-const member function; it is commented out. In line 38, pConstPtr calls SetAge(). pConstPtr is declared to be a constant pointer to a rectangle. In other words, the pointer is constant and cannot point to anything else, but the rectangle is not constant.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments