constant variables in c++

// various versions of const are explained below
#include <iostream>
class Entity {
private:
	int m_X, m_Y;
	mutable int var; // can be modified inside const menthods
	int* m_x, *m_y;// * use to create pointer in one line
public:
	int GetX() const // cant modify class variables
	{
		//m_X = 4;//error private member can't be modified inside const method
		var = 5; // was set mutable
		return m_X;
	}
	int Get_X()// will modify class 
	{
		return m_X;
	}
	const int* const getX() const  // returning a pointer that cannot be modified & context of pointer cannot be modified
	{
		//m_x = 4;
		return m_x;
	}
	void PrintEntity(const Entity& e) {
		std::cout << e.GetX() << std::endl;
	}
};
int main() {
	Entity e;
	const int MAX_AGE = 90;
   // MAX_AGE =100; error const var is stored in read only section in memory and we can't write to that memory
	//  int const* a = new int; is same as const int* a = new int ;////but you can't change the context of pointer but can reassign it to a pointer something else
	int * const a = new int; //can change the context of pointer but can't reassign it to a pointer something else
   *a = 2;
    a = &MAX_AGE;// error can't change it to ptr something else
   	std::cout << *a << std::endl;
	a =(int*) &MAX_AGE;
	std::cout << *a << std::endl;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
constant vs variables in c++ constant function in c++ set constant variable c++ constructor C++ using constants c++ make int constant define constant in cpp const in c++ for variable means c++ make constants declaring constant function in c++ declare const variable c++ constant variables can be created in c++ by using constant internal value c++ c++ syntax while declaring variables and named constants where is constants located in c++ defining a constant in cpp how to create constant variable in c++ and a global variable c++ constants in class constants and variables in c++ Constants, variables, expressions, operators, operations in c++ give constant to function c++ what is a constant in c++ explain with an example constant variable in class c++ two ways to declare constant in c++ c++ class constant variable how to make variable constant in c++ declare a constant variable in main c++ constant declaration in c++ define constant string inc++ c++ constants set constant variable c++ how to make a variable constant in c++ syntax of constant declaration c++ constant object in c++ const variables in c++ declare constant c++ constants in cpp c++ declare constant constant reference c++ hot to declare constant in c++ what is a constant int c++ constant variable on c++ c++ define constants constant in cpp constants in c++ different ways to declare constant in cpp when to use constant in cpp constant keyword in cpp constant varialble definition c++ static constant data in C++ what is a constant function in c++ named constant in c++ rules declaring constants in c++ cpp use variable as constant constant variable in class definition c++ constant variables c++ example geekfor geeks const in c++ c++ constant reference declaring cons variablet in cpp declaring const in cpp c++ declaring constants define constants in C++ variables in C++ c++ declare constant as float how to define constants in c++ setting constant in c++ how to declare a const variable in c++ how to make a constant int in c++ How are the constants declared? c++ c++ create constants decalring const int in c++ c++ constant variable how are constants declared in c++ constants in c++ language how to create a constant in c++ How to declare a constant in c++ set a constant int in c++ what is constant in c++ constant int c++ declare constant in c++ how to declare constants in c++ constant variables in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source