const 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;
}// CPP program to count words in a string
// using stringstream.
#include <bits/stdc++.h>
using namespace std;
int countWords(string str)
{
// breaking input into word using string stream
stringstream s(str); // Used for breaking words
string word; // to store individual words
int count = 0;
while (s >> word)
count++;
return count;
}
// Driver code
int main()
{
string s = "geeks for geeks geeks "
"contribution placements";
cout << " Number of words are: " << countWords(s);
return 0;
}
Also in C++:
- Title
- bellman ford algorithm cp algorithm
- Category
- C++
- Title
- how to initialize an struct object in c++
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- c++ class member initialization
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- C++ int to char*
- Category
- C++
- Title
- Application of c++ in youtube program
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- find vector in c++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- how read a shader from another file c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- traverse map c++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- goto c++
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- convert GLFWwindow* to IntPtr
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- how to run a c++ program in the background
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- c++ overloaded equality check operator
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- how to iterate through a map in c++
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- c++ program to find gcd of 3 numbers
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- c++ char print align
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- pointer related problems dangling/wild pointers c++
- Category
- C++
- Title
- stl sort in c++
- Category
- C++
- Title
- size of a matrix using vector c++
- Category
- C++
- Title
- how to inject a dll into a game c++
- Category
- C++
- Title
- mkdir c++
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- compare string c++
- Category
- C++
- Title
- max three values c++
- Category
- C++