const 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;
}

3.67
6
Tsai zi 115 points

                                    #define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'

3.67 (6 Votes)
0
3.75
8
BadHorsie 105 points

                                    //Syntax: const Class_Name Object_name;
const int MAXN = 1e5 + 5;
const long long mod = (long long)1e9 + 7;

3.75 (8 Votes)
0
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
what is const keyword in c++ what is a const method c++ what is a const function c++ c++ how to declare a const variable const keyword with function in c++ const expression c++ struct cpp how to declare const how to declare a const in c++ c++ define const int variable to const c++ #define constant in cpp what is the purpose of const in c++ constructor const value c++ what is constant function in c++ c++ what is the use of const c++ int const const in c++ for variable means c++ const c++ define const class c++ define const c++ const library c++ const int &amp;&amp; new constant int c++ C++ &quot;const &amp;&quot; meaning constant in CPP means const operator in c++ convert variable to const in c++ c++ what is const const function in class c++ different const with a function cpp const int cpp a const function c++ const function in c++ learncpp how to get constant e in c++ why const is used in c++ const variable c++ declaration what is const string in c++ how to set a const variable c++ declare const variable c++ const c++ keyword what is a const function in c++ return string const c++ what does you mean by const function in c++ const constructor c++ why const function c++ cpp constant in class const or define c++ practical example of const functions in c++ constant string c++ why do we use const in c++ c++ const variable = function() how declare a const c++ const variable in method c++ what is const function in c++ c++ const* const c++ define constant int c++ make variable const how to declare a function as const in c++ const in c++ meaning what is constant keyword in c++ c++ function const c++ constant int c++ const expression const in function signature c++ c++ const _elem cpp const* working cpp const* const T &amp; c++ const variable cpp what is const std in c++ const function cpp description() const in c++ c++ const at() function two ways to declare constant in c++ constant function in c++ c++ class constant string c++ class constant variable const function keyword c++ how to make variable constant in c++ whent to use const on function c++ what is the const in c++ const int in cpp\ const c++ in class const in C++ - part 1 declare constant in c+ cpp constantd can you change const value in cpp c++ const function what does const in methods mean in c++ c++ const stands for const class function c++ cpp function const const integer c++ c++ declare const function const &amp; c++ c++ const variable in class reference to const c++ what does const keyword mean in c++ const value in cpp const c++ explained what does const mean when returning c++ what does const mean c++ cpp const in function declaration const operator c++ how to change a const value c++ const c++reference c++ constant function what does a const function mean in c++ C++ const struct &amp; C++ const struct what is c++ const reference why use const in c++ const int c++ class constant declaration in c++ some const in c++ C++ math constants const in for loops c++ const constructor in c++ C++ const for funtion what is const return c++ create const int c++ c++ const variable in function how to define const function in c++ c++ const string c++ constant parameters purpose of const function c++ c++ what is an integer constant c++ constant in function c++ const instead of define c++ const functions cpp initiate constant const in a function c++ c++ constants c++ how to declare a constant set constant variable c++ const int in c++ const { c++ c++ class constants const c++ function c++ const variable how to make a variable constant in c++ when should you use const c++ constant function c++ const variable in cpp c++ const 1U c++ const U const int &amp; c++ const Ty c++ const const c++ c++ constant static variable how to make something to const c++ syntax of constant declaration c++ cpp constant strings const in cpp is applicable to wat does const mean c++ c++ whats const const parameter c++ constant object in c++ c++ constant in class how declare C++ const int why use const c++ c++ constant string constants C++ cpp constant const object c++ c++ constant class methods can you use const in expressions C++? c++ const meaning c++ const object c++ const function] const class c++ const variables in c++ what is const method cpp what is const cpp c++ const in constructor declare constant c++ const string cpp cpp const function in class define const object c++ c++ declare constant c++ declare const cpp declare const should i const everything c++ cpp new const const string c++ c++ const in function what const means in c++ constant reference c++ const specifier c++ const int&amp; c++ c++ const INT cpp constants c++ pi constant cpp const variable c++ create constant string define constants in c++ c++ class constant c++ constant variable const functions c++ const reference in c++ int *const means in cpp c++ const int&amp; c++ const &amp; when to use const in c++ const reference int c++ const variable in c++ const expresion c++ int const c ++ const int c++ c++ const keyword c++ const return value how to make a constant expression c++ declare const int c++ c++ take constants const var in c++ c++ constant example how to define constants in c++ how to declare a constant in c++ when to use constant in cpp for functions when to use constant in cpp how to define function as const in c++ const strings c++ c++ const explained where to declare constant in cpp using const in c++ c++ const method const method in c++ const string in cpp what's a constant expression c++ constant variable c++ const* const cpp why is const used in c++ use of const keyword in c++ c++ const int function how to declare constants in c++ c++ const * c++ const with parameters const in constructor c++ when to use const c++ const c+++ const example in c++ constants in c++ define const in c++ c++ concept is_const const en C++ const function c++ C++ const or defien? const function type in c++ const string &amp; c++ cpp constant methods when do we use const in functions in c++ what is const c++ what is a constant expression in c++ cpp const const int in c++\ reference const meaning c++ const string&amp; c++ const t&amp; c++ c++ const string&amp; const const keyword in c++ function function const cpp const and define in c++ how to make a constant variable in c++ c++ const = 0 plain const c++ C++ const methods function const c++ c++ define const variable using const in cpp const meaning c++ constant cpp meaning what does const do in c++ what does const do in c++ class is const a keyword in c++ const = 0 cpp const expression c++ what does const mean in c++ what is a const return value in c++ c++ what is const function where to declare constants in c++ when to use c++ const const c++ example when use const c++ what is a constant variable in c++ constant variables c++ example const variable c++ what is const in c++ c++ class const c++ constant reference cpp const function constant in cpp can const declared in main function what are valid literals in c++ cont keyword c++ const modifier c++ const member function c++ const class type c++ const functions in c++ const method c++ const member method c++ constants in cpp const c++ meaning how to declare const variable in c++ c++ what does const mean const reference c++ constant variable in c++ const in class c++ const variables c++ function() const c++ const function in c++ const c++ literal in cpp constents cpp #constant CPP what is a const method in c++ c++ const function const class in c++ constant class c++ const keyword c++ constant const in cpp constant keyword in c++ const keyword in cpp meaning of const in c++ const keyword c++ const data type c++ const variable meaning in c++ c++ const const keyword in c++ what is const in c++ const cpp const 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