Need a project done?

C++ Programming Developer

Search This Blog

Constant objects require constant functions C++


#include<iostream>
using namespace std;

class A{
int i;
public:
A(){ i = 0; };
int c_get_i() const{ return i; }
int nc_get_i(){ return i; }
};

int main(){
const A c_a;   // c_a is a const object
A nc_a;

c_a.c_get_i();
//c_a.nc_get_i(); // This would give error if uncommented because const objects require const functions

nc_a.c_get_i();
nc_a.nc_get_i();
}

Written by Dr. Shahzad Rajput
"Don't let anyone ever make you feel like you don't deserve what you want."