简体   繁体   English

C ++:如何访问内部类的方法?

[英]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: 问题:

  1. Why can't I access the method 'method2 from objects of class myClass`? 为什么不能from objects of class myClass from objects of class访问方法“ method2”?
  2. Is there a way to access 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. 对于第二个问题,我想创建类的对象的myClass2myClass ,然后让另一个函数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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM