For given nested classes in C++, what will be the output of the following code:
#include <iostream> 
using namespace std;

class A {
public:
    int a;
    int getB() {
        return B::b;
    }
    class B {
    public:
                B() {
            b = 1;
        }
        int b;
    };
};

int main() 
{
    A a;
    cout << a.getB();
    return 0;
}
Explanation
_Non-static_ class fields can only be accessed through a class instance.
In member function 'int A::getB()':
 error: invalid use of non-static data member 'A::B::b'
         return B::b;
                   ^

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes