array of pointer and nested class

Hi, what is the difference when I call the first element (ptr[0]) of the two arrays, respectively:
someclass *ptr = arr[3]

someclass *ptr[3] (Then initialize each ptr with "new")

Another question is for nested class, can the outer class call the private data member or function of the inner class?


Thanks!
This sounds like a homework question, but...
1
2
3
4
5
someclass *ptr = arr[3]; //this is the same as

someclass *ptr; //creates a pointer (to a someclass object)
ptr = arr[3]; //takes the pointer in arr[3] and stores it in ptr
              //(this means the 4th element of the array "arr" is a pointer) 
1
2
3
4
5
6
someclass *ptr[3]; //creates an array of someclass pointers (length 3)

//i.e.
ptr[0]; //refers to a someclass pointer
*ptr[0]; //refers to a someclass object
ptr; //is a pointer to a someclass pointer (the first in the array of someclass pointers) 

Yeah, thank you Mathhead! I feel stupid. Then what about the second question?
I've never tried that in C++ without declaring the classes as friends, by in java the answer would be yes. Just whip up some test code like:
1
2
3
4
5
6
7
8
9
10
11
12
class Outer {
    class Inner {
     private:
        int field;
        void method();
    } inner;
} outer;

int main() {
    outer.inner.field;
    outer.inner.method();
}
Last edited on
The outcome turned out to be negative, thanks!
Topic archived. No new replies allowed.