By Sabetay Toros


2018-08-10 11:23:49 8 Comments

Is it possible to use a variable in an inner class declared in Outer class . I would like to achieve like the following. Is it possible. I am getting the following error.

prog.cc: In constructor 'Outer::Inner::Inner()': prog.cc:12:25: error: invalid use of non-static data member 'Outer::i' Inner( ) { i = 5; };

    #include <iostream>
using namespace std;
class Outer {
public:
    int i;
    class Inner; // forward declaration of Outer::Inner
    friend class Inner;
    class Inner {
        Inner() {
            i = 5;
        };
    };
};
int main() {
    return 0;
}

2 comments

@user846834 2018-08-10 11:59:34

From the working draft of the standard available online:

9.7 Nested class declarations [class.nest]
A class can be declared within another class. A class declared within another is called a nested class. The name of a nested class is local to its enclosing class. The nested class is in the scope of its enclosing class.

Example:

int x; 
int y;
struct enclose {  
    int x; 
    static int s;
    struct inner { 
        void f(int i) { 
            int a = sizeof(x); // OK: operand of sizeof is an unevaluated operand 
            x = i; // error: assign to enclose::x 
            s = i; // OK: assign to enclose::s 
            ::x = i; // OK: assign to global x 
            y = i; // OK: assign to global y 
        } 
        void g(enclose* p, int i) { 
            p->x = i; // OK: assign to enclose::x 
        } 
    };     
}; 
inner* p = 0; // error: inner not in scope 

As you can see from the example provided in the document, the only way for a nested class to access a non-static member of the enclosing class is through a pointer to the enclosing class.

That is what happens in void g(enclose* p, int i)

@Botje 2018-08-10 11:25:36

Unlike Java, C++ "inner classes" have no connection to the outer class that created them. You will have to pass in a pointer or reference to the outer class.

@Sabetay Toros 2018-08-10 11:30:53

I think this is a flaw for C++ language.

@Sabetay Toros 2018-08-10 11:32:31

Whats the use to use inner class ?

@Tyker 2018-08-10 11:34:08

@SabetayToros the types of classes aran't related but you the inner class is in the namespace of the inner class. you would need to do outer::inner to access the inner class

@KorelK 2018-08-10 11:34:49

@SabetayToros the access scope is change.

@Jonathan Wakely 2018-08-10 11:35:13

Don't be so quick to call something a flaw in a language you don't understand.

@user463035818 2018-08-10 11:35:35

@SabetayToros if you call this a flaw then I'd like to see your list of flaws in Java ;)

@Botje 2018-08-10 11:35:37

An inner class is like any other class, except it is nested under the outer class and thus has a visible association with it. You could create a HashMap where certain methods return a HashMap::Entry containing a key-value pair, for example. If this Entry would invisibly contain a reference to the HashMap that created it, you suddenly cannot just copy Entry objects around or return them (the HashMap might be stack-allocated, for example)

@user463035818 2018-08-10 11:37:42

@SabetayToros one use of inner classes is being able to declare stuff in the scope where it belongs, eg you can have container::iterator (where actually it doesnt matter whether it is just a typedef of an inner class)

@Clonk 2018-08-10 11:37:43

Trying to apply Java principle to C++ will only confuse you. Read this answer, it will help you achive what you want

@Sabetay Toros 2018-08-10 13:04:38

user463035818 please dont be offend. I need some global variables that must be shared among very tiny structures. I´m lazy and I don´t want to write every time I have to access to this variables specifying the owning variable. I like the basic principle of C++. language that gives me the freedom to write everything without restrictions.. IMHO this is a restriction.

Related Questions

Sponsored Content

33 Answered Questions

10 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 419235 View
  • 1503 Score
  • 10 Answer
  • Tags:   c++ unix profiling

10 Answered Questions

[SOLVED] C++ static constant string (class member)

23 Answered Questions

[SOLVED] Why can't variables be declared in a switch statement?

15 Answered Questions

[SOLVED] How do you declare an interface in C++?

1 Answered Questions

C++ namespaces, inner classes and operator resolution

6 Answered Questions

[SOLVED] Initializing const member within class declaration in C++

  • 2012-02-04 15:29:26
  • ezpresso
  • 56797 View
  • 64 Score
  • 6 Answer
  • Tags:   c++ g++

2 Answered Questions

[SOLVED] C++: Forward declaration of class member (variable)

  • 2016-01-07 06:22:59
  • AYO903490823490
  • 562 View
  • 1 Score
  • 2 Answer
  • Tags:   c++

3 Answered Questions

[SOLVED] How do I forward declare an inner class?

Sponsored Content