In case of nested class, how do I access the "Inner" or "Child" class's member function?. For example, the code, where I created "obj1". Now how do I access the "childPrint()" with"obj1"?
example_code:
#include<iostream>
using namespace std;
/////////////////////////////////////////////////
class Parent
{
public:
void print()
{
cout<<"this is from Parent"<<endl;
}
class Child
{
public:
void childPrint()
{
cout<<"this is from Child"<<endl;
}
};
};
int main()
{
Parent obj1;
obj1.print();
Parent::Child obj2;
obj2.childPrint();
obj1.Child::childPrint();///ERROR
return 0;
}
obj1.Child::childPrint();
In this particular line, you need to understand that childPrint()
is an instance member function of class Child
. So, only an instance of class Child
can call childPrint()
function.
If childPrint()
function is a static member function(class function) of class Child
then it is possible to call it without creating any instance and no error will be shown in Parent::Child::childPrint();
.