+ 1
regVar and constVar are in private, how can they get access?
Normally, when members of the class are in private, they cannot get access, but I really don't get it about this code: #include <iostream> using namespace std; class MyClass { public: MyClass(int a, int b); private: int regVar; const int constVar; }; MyClass::MyClass(int a, int b) : regVar(a), constVar(b) { cout << regVar << endl; cout << constVar << endl; } int main() { MyClass obj(42, 33); } //Output 42 33
1 Resposta
+ 2
If Im not wrong that is the constructor of the class. So a class via its constructor can access its private members.