C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, a const
object is an instance of a class that cannot be modified after it is created. This tutorial will show you how to create and work with const
objects in C++.
Creating const Objects
To create a const
object, use the const
keyword before the class name when declaring the object.
Example:
class MyClass { public: int x; MyClass(int a) : x(a) {} int getX() const { return x; } }; int main() { const MyClass obj(10); // Create a const object return 0; }
In this example, we create a const
object obj
of the MyClass
class. Since obj
is a const
object, its data members cannot be modified after it is created.
Working with const Objects
const
object. To access the data members, you need to use const
member functions.int main() { const MyClass obj(10); // obj.x = 20; // Error: Cannot modify a const object std::cout << "x: " << obj.getX() << std::endl; // Access data member using a const member function return 0; }
const
member functions on a const
object. Non-const
member functions are not allowed because they might modify the object.class MyClass { public: int x; MyClass(int a) : x(a) {} int getX() const { return x; } void setX(int a) { x = a; } }; int main() { const MyClass obj(10); std::cout << "x: " << obj.getX() << std::endl; // Allowed: Calling a const member function // obj.setX(20); // Error: Cannot call a non-const member function on a const object return 0; }
Why use const Objects?
Using const
objects has several benefits:
Complete Example
Here's a complete example demonstrating the use of const
objects in C++:
#include <iostream> class MyClass { public: int x; MyClass(int a) : x(a) {} int getX() const { return x; } void setX(int a) { x = a; } }; int main() { const MyClass constObj(10); // Create a const object MyClass nonConstObj(20); // Create a non-const object std::cout << "constObj.x: " << constObj.getX() << std::endl; // Access data member using a const member function std::cout << "nonConstObj.x: " << nonConstObj.getX() << std::endl; // constObj.setX(30); // Error: Cannot call a non-const member function on a const object nonConstObj.setX(40); // Allowed: Calling a non-const member function on a non-const object return 0; }
Creating and using const objects in C++:
#include <iostream> class MyClass { public: int data; MyClass(int value) : data(value) {} }; int main() { const MyClass obj(42); // Creating a const object std::cout << obj.data << std::endl; // Accessing data in a const object // obj.data = 10; // Error: Cannot modify data in a const object return 0; }
Const member functions and const objects in C++:
#include <iostream> class MyClass { public: int data; MyClass(int value) : data(value) {} void display() const { std::cout << "Data: " << data << std::endl; } }; int main() { const MyClass obj(42); obj.display(); // Calling a const member function on a const object // obj.data = 10; // Error: Cannot modify data in a const object return 0; }
Const reference to objects in C++:
#include <iostream> class MyClass { public: int data; MyClass(int value) : data(value) {} }; int main() { const MyClass obj(42); const MyClass& refObj = obj; // Const reference to a const object std::cout << refObj.data << std::endl; // refObj.data = 10; // Error: Cannot modify data through a const reference return 0; }
C++ const object vs const pointer:
#include <iostream> class MyClass { public: int data; MyClass(int value) : data(value) {} }; int main() { const MyClass obj(42); const MyClass* constPtr = &obj; // Const pointer to a const object std::cout << constPtr->data << std::endl; // constPtr->data = 10; // Error: Cannot modify data through a const pointer return 0; }