Class contents

When going through lines of c++ code, I constantly find code that just doesn't seem to be effecient to me. Is there any advantage to using this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class foo
{
private:
  int i;
public:
  int Get_i()
  { 
    return i; 
  }
  void Set_i(int input) 
  {
    i = input;
  }
};


Over this code:
1
2
3
4
5
class goo
{
public:
  int i;
};


People have told me that it is a "protection" thing, but that doesn't seem right since a coder could access 'i' either way (just more indirectly in one instance).
This is not inefficient since methods defined inside a class are by default inline. In your example case, this is indeed useless though. A reason to do this would be to add some error checking code insider the getters and setters.
Ahhh that makes sense. It is just for contingency in error checking. Thank you.
Topic archived. No new replies allowed.