constant variables in c++
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;
}
Also in C++:
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- string length c++
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- c++ program to find gcd of 3 numbers
- Category
- C++
- Title
- Find the minimum difference between pairs in a simple path of tree C++
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- compare values within within a vector c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- minmax_element c++
- Category
- C++
- Title
- how to make sure the user inputs a int and not anything else c++
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- c++ while loop code
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- find all the palindrome substring in a given string
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- how to know the correct class of objects cpp
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- switch case sinax c++
- Category
- C++
- Title
- elseif c++
- Category
- C++
- Title
- size of a matrix using vector c++
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- how to put a class in a .h file c++
- Category
- C++
- Title
- while loops
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- vector of string in c++
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- convert to lowercase c++
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- sum of vector c++
- Category
- C++
- Title
- How to read a file in in C++
- Category
- C++
- Title
- c++ overloaded equality check operator
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- c++ bubble sort array
- Category
- C++