C++: How can I access a method of an inner class?

m0bi5

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:

  1. Why can't I access the method 'method2from objects of classmyClass`?
  2. Is there a way to access 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.

juanchopanza

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ How can I access to an inner enum class?

From Dev

How can I access an inner class?

From Dev

Can I access the "secondary this" from an inner class?

From Dev

C#: How can I instantiate a List of private inner class?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Can I reference a class from a method within an inner class, and if so, how?

From Dev

How can I access injected Grails beans in an abstract class method?

From Dev

Android:How can I access a value from a method of a class?

From Dev

How can I access a method defined with new keyword in a derived class

From Dev

How i can access inner class variable value using reflection in java?

From Dev

How can I replace an Observable anonymous inner class by lambda, including a logger, for multiple Couchbase access?

From Dev

How can I access the inner HTML of an element while looping over it with a class selector?

From Java

How can I change value of variable in side method from anonymous inner class?

From Dev

How can I change value of variable in side method from anonymous inner class?

From Dev

How do I access an inner class constructor from a derived class?

From Dev

I can't access a method in a class

From Dev

Why can't I create a new method in an anonymous inner class?

From Dev

C# how can I get generic class reference at method

From Dev

How can i ask for class type of inner class?

From Dev

How can I access a class data member from a method within the same class?

From Dev

How to access a class which have the same name as the Method Local Inner class of a different class

From Dev

Why can't I access a public string from a method within the same class C#

From Dev

How can I add a value to an ArrayList from an inner method?

From Dev

How to access self in class method objective C

From Dev

TypeScript - How can I access "this" in async method

From Dev

how can i access vuejs data in method

From Dev

How can I tell which constructor to use on an inner (member) class?

From Dev

How can I pass a variable to an anonymous inner class in Java?

Related Related

  1. 1

    C++ How can I access to an inner enum class?

  2. 2

    How can I access an inner class?

  3. 3

    Can I access the "secondary this" from an inner class?

  4. 4

    C#: How can I instantiate a List of private inner class?

  5. 5

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  6. 6

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  7. 7

    Can I reference a class from a method within an inner class, and if so, how?

  8. 8

    How can I access injected Grails beans in an abstract class method?

  9. 9

    Android:How can I access a value from a method of a class?

  10. 10

    How can I access a method defined with new keyword in a derived class

  11. 11

    How i can access inner class variable value using reflection in java?

  12. 12

    How can I replace an Observable anonymous inner class by lambda, including a logger, for multiple Couchbase access?

  13. 13

    How can I access the inner HTML of an element while looping over it with a class selector?

  14. 14

    How can I change value of variable in side method from anonymous inner class?

  15. 15

    How can I change value of variable in side method from anonymous inner class?

  16. 16

    How do I access an inner class constructor from a derived class?

  17. 17

    I can't access a method in a class

  18. 18

    Why can't I create a new method in an anonymous inner class?

  19. 19

    C# how can I get generic class reference at method

  20. 20

    How can i ask for class type of inner class?

  21. 21

    How can I access a class data member from a method within the same class?

  22. 22

    How to access a class which have the same name as the Method Local Inner class of a different class

  23. 23

    Why can't I access a public string from a method within the same class C#

  24. 24

    How can I add a value to an ArrayList from an inner method?

  25. 25

    How to access self in class method objective C

  26. 26

    TypeScript - How can I access "this" in async method

  27. 27

    how can i access vuejs data in method

  28. 28

    How can I tell which constructor to use on an inner (member) class?

  29. 29

    How can I pass a variable to an anonymous inner class in Java?

HotTag

Archive