[英]C++: How can I access a method of an inner class?
I was going through classes
and thought of doing nested classes.So I have the below class declaration. 我正在遍历
classes
并想到做嵌套类,因此我有以下类声明。
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
有一个method2
方法。下面的代码工作得很好,并为我提供了所需的输出。
myClass obj;
obj.method1();
But when I try this: 但是当我尝试这个:
obj.method2();
I get the error message method2 is not a member of myClass
. 我收到错误消息
method2 is not a member of myClass
。
Questions: 问题:
from objects of class
myClass`? from objects of class
myClass from objects of class
访问方法“ method2”? method2
? 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. 对于第二个问题,我想创建类的对象的
myClass2
在myClass
,然后让另一个函数myClass
访问method2
功能。
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
. 1)因为
Class2()
不是myClass
的成员。
2) You need to instantiate a myClass::myClass2
object and call the member on it. 2)您需要实例化一个
myClass::myClass2
对象并在其上调用成员。 myClass2::Class2()
is a member function, so it needs an object to act on. myClass2::Class2()
是成员函数,因此需要一个对象来对其进行操作。 For example 例如
myClass::myClass2 obj;
obj.Class2();
Note that this requires that you make myClass2::Class2()
a public member. 请注意,这要求您将
myClass2::Class2()
为公共成员。 It is private in your code. 它在您的代码中是私有的。
Putting class2
inside of class1
does less than you think it does. 把
class2
的内部class1
不小于你认为它。 It does nothing more than put it in class1
's scope. 它所做的无非就是将其置于
class1
的范围内。
So, imagine the same thing but not nested: 因此,请想象同一件事,但不要嵌套:
class myClass
{
public:
void Class1()
{
cout<<"This is Class 1"<<endl;
}
};
class myClass2
{
public:
void Class2()
{
cout<<"This is Class 2"<<endl;
}
};
...
// and this should look normal to you:
myClass obj;
obj.Class1();
myClass2 obj2;
obj2.Class2();
Now put myClass2
in myClass
scope, like you did, and see how the last few lines change. 现在,像您一样将
myClass2
放入myClass
范围,并查看最后几行的变化。
class myClass
{
public:
void Class1()
{
cout<<"This is Class 1"<<endl;
}
class myClass2
{
public:
void Class2()
{
cout<<"This is Class 2"<<endl;
}
};
};
...
myClass obj;
obj.Class1();
myClass::myClass2 obj2;// this is the only thing that changes. You put myClass2 in a different scope.
obj2.Class2();
So to answer your questions directly: 因此,直接回答您的问题:
1) Why cant I access the function Class2 using the myClass obj.
1)为什么我不能使用myClass obj访问函数Class2。
Because Class2
is not a member of myClass
. 因为
Class2
不是myClass
的成员。 It's a member of myClass2
, which you need to instantiate just like you did with myClass
. 它是
myClass2
的成员,您需要像对myClass
一样进行实例化。
2) Is there a way to access Class2 function.
2)有没有一种方法可以访问Class2函数。
Yes, by instantiating a myClass2
, like at the end of my example: myClass::myClass2 obj2; obj2.Class2();
是的,通过实例化
myClass2
,例如在我的示例结尾: myClass::myClass2 obj2; obj2.Class2();
myClass::myClass2 obj2; obj2.Class2();
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.