Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Access data from an inner class

Reply
Thread Tools

Access data from an inner class

 
 
alexo
Guest
Posts: n/a
 
      03-21-2013
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;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-21-2013
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
 
Reply With Quote
 
 
 
 
alexo
Guest
Posts: n/a
 
      03-21-2013
> 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:

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.


 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      03-21-2013
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:
>
> 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.
 
Reply With Quote
 
alexo
Guest
Posts: n/a
 
      03-21-2013

>
> 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 )
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      03-22-2013
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 )


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

Bart v Ingen Schenau
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      03-31-2013
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.
 
Reply With Quote
 
Tobias Müller
Guest
Posts: n/a
 
      04-01-2013
terminator <(E-Mail Removed)> 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
 
Reply With Quote
 
alexo
Guest
Posts: n/a
 
      04-01-2013
>
> 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();
}

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-01-2013
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: 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access to private data type of an outer class from inner class agentprog@gmail.com C++ 2 04-02-2013 04:48 PM
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments