Skip to content

Instantly share code, notes, and snippets.

@rudradesai200
Last active February 6, 2020 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudradesai200/0249555f5d80a3c0aa4d305214c1871c to your computer and use it in GitHub Desktop.
Save rudradesai200/0249555f5d80a3c0aa4d305214c1871c to your computer and use it in GitHub Desktop.
Code used in my first medium article. Check it out here https://medium.com/@rudra.desai200/how-to-use-const-keyword-in-c-b65ee42cf09
class Dummy {
private:
int X = 0;
public:
//this function will give an error if
//object declared is of constant type
int getX() { return X; }
//this function will work fine as
//we have declared it const.
void printX() const { std::cout<<X<<std::endl; }
};
int main(){
const Dummy d;
// d.getX(); //it will return a compilation error if uncommented
d.printX(); //it will work just fine
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment