c++private-membersclass-members

Accessibility of data member in member function before declaration of data member


Consider this code:

class Test
{
public:

Test() { i = 0;  }

private:

int i;
};

Data member 'i' is used even before it is declared/defined. Should this not be a compilation error ? (It compiled fine!!!)


Solution

  • The rule is that member functions defined in the class definition are compiled as if they were defined immediately after the class definition.