I was going through classes
and thought of doing nested classes.So I have the below class declaration.
class myClass {
public:
void method1() {
cout << "This is method 1" << endl;
}
class myClass2 {
void method2() {
cout << "This is method 2" << endl;
}
};
};
As you can see , myClass
has a method method2
in it.The code below works just fine and gives me the desired output.
myClass obj;
obj.method1();
But when I try this:
obj.method2();
I get the error message method2 is not a member of myClass
.
Questions:
from objects of class
myClass`?method2
?For Question 2 , I thought of creating an object of class myClass2
in myClass
, then make another function in myClass
to access the method2
function.
I want to know if there is any other way out, because my way seems a bit pain staking.
1) Because Class2()
is not a member of myClass
.
2) You need to instantiate a myClass::myClass2
object and call the member on it. myClass2::Class2()
is a member function, so it needs an object to act on. For example
myClass::myClass2 obj;
obj.Class2();
Note that this requires that you make myClass2::Class2()
a public member. It is private in your code.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments