Ad Code

Const member Functions ?

In the following snippet of code even if i declare getName() function as const; then i cannot use getName() function in other non-const functions; it means that using const keyword is creating more problems for me rather than resolving; kindly put some light on the issue?

class Student{
char * name;
public:
char *getName();
void setName(char * aName);
int ConstFunc() const{
name = getName(); //error
setName(“Ahmad”);//error
}
};


It is obvious due to following properties of const function:

    Constant member functions cannot modify the state of any object
    They are just “read-only”
    Errors due to typing are also caught at compile time
Reactions

Post a Comment

0 Comments