Const member functions

In the previous example, we kept the struct very simple. Since the struct didn't have any member functions, we only needed to worry about when non-member functions want to modify the data. However, object-oriented programming suggests that we shouldn't have data be public. Instead, all data should be private and accessed through public member functions. Let's look at a very simple example to understand this concept:

class Simple { public: Simple(void) {   m_data = 0;   } void SetData(int data) {   m_data = data; } int GetData(void) {   return m_data;   } private: int m_data; };  int main(void) { Simple s; const Simple cs;  s.SetData(10);          //Works as Expected int value = s.GetData();//Works as Expected  cs.SetData(10); //Error ...

Get Game Development Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.