Access data from an inner class

Discussion in 'C++' started by alexo, Mar 21, 2013.

  1. alexo

    alexo Guest

    Hello,
    I don't know very much about inner classes, I'm still learning
    the subject. Playing with the compiler and searching for a solution
    on the Web I have written the following code.
    In it I successfully try to access a private member of an outer
    class from the inside of an inner class. [I do that because of I heard
    that inner classes could access outer's data member
    either public or private].

    I know there are many ways to accomplish the same task in computer
    programming especially for languages as powerful and flexible as C++,
    so my asking was if there were other methods to accomplish the same
    task. Two solutions came to my mind, namely

    make the inner class a friend of the outer class
    make the inner class to inherit from outer

    but both ways I was unable to properly set up.
    Could you tell me if these two methods can be successfully be applied?
    Thank you.

    *cut here*

    #include <iostream>

    using std::cout;
    using std::endl;

    class Outer
    {
    private:
    int x; // the member I would like to access from
    // Inner class

    public:
    Outer() {}
    ~Outer() {}

    void show()
    {
    cout << "the value of x is " << x << endl;
    }

    class Inner
    {
    public:
    Outer *o;
    Inner(Outer * p_out) {o = p_out;}
    ~Inner() {}

    void setx(int val)
    {
    o->x = val;
    }
    };
    };


    int main()
    {
    Outer A;

    Outer::Inner B(&A);

    B.setx(3); // set Outer's x to 3
    A.show();

    return 0;
    }
     
    alexo, Mar 21, 2013
    #1
    1. Advertising

  2. On 3/21/2013 5:01 PM, alexo wrote:
    > I don't know very much about inner classes, I'm still learning
    > the subject.


    .... before you go any further... Let's begin with this fact: inner
    class is a definition of a class inside the definition of another class.
    It does *not* create any special relationship between *objects* of
    those classes *unless* you have done it yourself. The relationship
    exists only between *types*.

    > Playing with the compiler and searching for a solution
    > on the Web I have written the following code.
    > In it I successfully try to access a private member of an outer
    > class from the inside of an inner class. [I do that because of I heard
    > that inner classes could access outer's data member
    > either public or private].


    Since inner classes are *members* of the outer class, they have access
    to all other members of that class, yes. Just like member functions of
    the class have access to other members of the same class. The reverse
    is not true, if memory serves. The members of the outer class cannot
    access private or protected members of the inner class without being
    friends of the inner class.

    > I know there are many ways to accomplish the same task in computer
    > programming especially for languages as powerful and flexible as C++,
    > so my asking was if there were other methods to accomplish the same
    > task.


    What's the task? Accessing the member is the mean, what's the goal?

    > Two solutions came to my mind, namely
    >
    > make the inner class a friend of the outer class
    > make the inner class to inherit from outer
    >
    > but both ways I was unable to properly set up.
    > Could you tell me if these two methods can be successfully be applied?


    I am not sure how you'd make the inner class derive from the outer, but
    friendship can be established in either direction.

    class outer {
    friend class inner;

    class inner {
    friend class outer;
    static void foo() { return outer::boo(); }
    };

    void bar() { return inner::foo(); }
    static void boo();
    };

    > Thank you.
    >
    > *cut here*
    >
    > #include <iostream>
    >
    > using std::cout;
    > using std::endl;
    >
    > class Outer
    > {
    > private:
    > int x; // the member I would like to access from
    > // Inner class
    >
    > public:
    > Outer() {}
    > ~Outer() {}
    >
    > void show()
    > {
    > cout << "the value of x is " << x << endl;
    > }
    >
    > class Inner
    > {
    > public:
    > Outer *o;
    > Inner(Outer * p_out) {o = p_out;}
    > ~Inner() {}
    >
    > void setx(int val)
    > {
    > o->x = val;
    > }
    > };
    > };
    >
    >
    > int main()
    > {
    > Outer A;
    >
    > Outer::Inner B(&A);
    >
    > B.setx(3); // set Outer's x to 3
    > A.show();
    >
    > return 0;
    > }


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 21, 2013
    #2
    1. Advertising

  3. alexo

    alexo Guest

    > Since inner classes are *members* of the outer class, they have access
    > to all other members of that class, yes. Just like member functions
    > of the class have access to other members of the same class. The
    > reverse is not true, if memory serves. The members of the outer class
    > cannot access private or protected members of the inner class without
    > being friends of the inner class.


    If you remove the pointer to the Outer class from the Inner class and
    give to Inner a default constructor instead of the one coded in my
    example when you try to access x directly, the compiler says:

    In member function ‘void Outer::Inner::setx(int)’:
    error: invalid use of nonstatic data member ‘Outer::x’

    So It is not true you can access data members of Outer from inside Inner.

    > What's the task? Accessing the member is the mean, what's the goal?


    Have you ever attended a science class when you were at school?
    your chemistry teacher probably told you that an acid and a base make
    a salt and that the resulting solution heats and you probably wanted
    to test it by your own in the lab [or worst at home], just to fix your
    ideas.
    Ok my example probably doesn't fit good, but I hope it explains why I
    have to try that statement. Yes, the task is try to access the member
    of the outer class directly from within the inner class. I was not
    able to do it without that tricky pointer to the Outer class made it
    be pointed to an instance of the Outer class.

    > > Two solutions came to my mind, namely
    >>
    >> make the inner class a friend of the outer class
    >> make the inner class to inherit from outer
    >>
    >> but both ways I was unable to properly set up.
    >> Could you tell me if these two methods can be successfully be applied?

    >
    > I am not sure how you'd make the inner class derive from the outer,


    the compiler doesn't let me derive Inner from Outer but I thought
    it a fault in my code.

    > but friendship can be established in either direction.


    I know that, but in this case too, I was not be able to access x
    directly.
     
    alexo, Mar 21, 2013
    #3
  4. alexo

    Öö Tiib Guest

    On Friday, 22 March 2013 01:25:24 UTC+2, alexo wrote:
    > If you remove the pointer to the Outer class from the Inner class and
    > give to Inner a default constructor instead of the one coded in my
    > example when you try to access x directly, the compiler says:
    >
    > In member function ‘void Outer::Inner::setx(int)’:
    > error: invalid use of nonstatic data member ‘Outer::x’
    >
    > So It is not true you can access data members of Outer from inside Inner.


    You have access to *private* members. It does not mean you can use them
    in invalid way. Consider this example:

    struct X { int m; };

    int main()
    {
    X::m = 42; // error: invalid use of nonstatic data member ‘X::m’
    }

    Here I try to use non-static data member without having object of type X.
    Like you did. That is invalid. Invalid despite i have full access, since all
    members of struct are public by default.
     
    Öö Tiib, Mar 21, 2013
    #4
  5. alexo

    alexo Guest


    >
    > You have access to *private* members. It does not mean you can use them
    > in invalid way. Consider this example:
    >
    > struct X { int m; };
    >
    > int main()
    > {
    > X::m = 42; // error: invalid use of nonstatic data member ‘X::m’
    > }
    >
    > Here I try to use non-static data member without having object of type X.
    > Like you did. That is invalid. Invalid despite i have full access, since all
    > members of struct are public by default.
    >


    So the correct way to use x is the way I coded in the first post
    right? id est instantiate Outer inside of Inner and access its
    variable x through that object.
    I'm sorry, I'm still learning :eek:)
     
    alexo, Mar 21, 2013
    #5
  6. On Fri, 22 Mar 2013 00:47:01 +0100, alexo wrote:


    >> You have access to *private* members. It does not mean you can use them
    >> in invalid way. Consider this example:
    >>
    >> struct X { int m; };
    >>
    >> int main()
    >> {
    >> X::m = 42; // error: invalid use of nonstatic data member ‘X::m’
    >> }
    >>
    >> Here I try to use non-static data member without having object of type
    >> X. Like you did. That is invalid. Invalid despite i have full access,
    >> since all members of struct are public by default.
    >>
    >>

    > So the correct way to use x is the way I coded in the first post right?
    > id est instantiate Outer inside of Inner and access its variable x
    > through that object.


    Yes, that is right.
    In particular, creating an object of type Outer::Inner does *not*
    magically cause an object of type Outer to come into existence. For that
    reason, you must tell the Outer::Inner object from which Outer object it
    should access the members.

    > I'm sorry, I'm still learning :eek:)


    No worries. That is part of why we are here.

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Mar 22, 2013
    #6
  7. alexo

    terminator Guest

    victor is too C++ ish OP you apparently has come from java.
    you can not access any member without prefixing the instance holding them.
    the only exception is that in none static member functions you can omit the implied "this".
    inner class is supposed to provide an interface to the nesting class, every imaginable implementation of what OP may want is - by design - similar to what
    he has done. But I`d consider googling for "mixins" for some further insight.

    regards,
    FM.
     
    terminator, Mar 31, 2013
    #7
  8. terminator <> wrote:
    > victor is too C++ ish OP you apparently has come from java.


    I don't think so.

    > you can not access any member without prefixing the instance holding them.


    He didn't.

    > the only exception is that in none static member functions you can omit the implied "this".


    In Java that's exactly the same as in C++. And certainly it's not the
    problem here.

    > inner class is supposed to provide an interface to the nesting class,
    > every imaginable implementation of what OP may want is - by design -
    > similar to what he has done. But I`d consider googling for "mixins" for
    > some further insight.


    I don't think that mixins are in any way relevant here.

    Did you actually understand the question?

    Tobi
     
    Tobias Müller, Apr 1, 2013
    #8
  9. alexo

    alexo Guest

    >
    > I know there are many ways to accomplish the same task in computer
    > programming especially for languages as powerful and flexible as C++,
    > so my asking was if there were other methods to accomplish the same
    > task.


    Here I've found a more straightforward [at least in my opinion]
    to access an outer class's member from within the inner class.
    Any comment about?

    *cut here*

    #include <iostream>

    using std::cout;
    using std::endl;

    class Outer
    {
    private:
    int x; // the member I want to access

    class Inner
    {
    public:
    Inner(Outer &o, int value)
    {
    o.x = value;
    }
    };

    public:
    Outer(int value)
    {
    Inner i(*this, value);
    }
    ~Outer() {}

    void show() { cout << "x value = " << x << endl; }

    };

    int main()
    {
    Outer A(1);
    A.show();
    }
     
    alexo, Apr 1, 2013
    #9
  10. On 4/1/2013 4:38 AM, alexo wrote:
    >>
    >> I know there are many ways to accomplish the same task in computer
    >> programming especially for languages as powerful and flexible as C++,
    >> so my asking was if there were other methods to accomplish the same
    >> task.

    >
    > Here I've found a more straightforward [at least in my opinion]
    > to access an outer class's member from within the inner class.
    > Any comment about?
    >
    > *cut here*
    >
    > #include <iostream>
    >
    > using std::cout;
    > using std::endl;
    >
    > class Outer
    > {
    > private:
    > int x; // the member I want to access
    >
    > class Inner
    > {
    > public:
    > Inner(Outer &o, int value)
    > {
    > o.x = value;
    > }
    > };
    >
    > public:
    > Outer(int value)
    > {
    > Inner i(*this, value);
    > }
    > ~Outer() {}
    >
    > void show() { cout << "x value = " << x << endl; }
    >
    > };
    >
    > int main()
    > {
    > Outer A(1);
    > A.show();
    > }


    The code looks OK. Do you have any problems compiling it? From what I
    understand about inner members, the access to Outer::x in the c-tor of
    Inner should be allowed (since 'Inner' is a member of 'Outer'). I do
    seem to recall that in some compilers earlier this century such access
    might cause problems since those compilers did not recognize inner
    classes as members. VC++ was one of those, methinks.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 1, 2013
    #10
  11. alexo

    alexo Guest

    Il 01/04/2013 14:55, Victor Bazarov ha scritto:
    > On 4/1/2013 4:38 AM, alexo wrote:
    >>>
    >>> I know there are many ways to accomplish the same task in computer
    >>> programming especially for languages as powerful and flexible as C++,
    >>> so my asking was if there were other methods to accomplish the same
    >>> task.

    >>


    > The code looks OK. Do you have any problems compiling it? From what
    > I understand about inner members, the access to Outer::x in the c-tor
    > of Inner should be allowed (since 'Inner' is a member of 'Outer').


    No problem compiling or executing that code. I wanted just an opinion
    on my 2nd solution to the problem I rose in this thread
     
    alexo, Apr 1, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  2. Carlo v. Dango
    Replies:
    14
    Views:
    1,066
    Alex Martelli
    Oct 19, 2003
  3. Pyenos
    Replies:
    2
    Views:
    400
    Pyenos
    Dec 27, 2006
  4. Robin
    Replies:
    0
    Views:
    428
    Robin
    Jun 6, 2007
  5. Replies:
    2
    Views:
    245
Loading...

Share This Page