Access data from an inner class

A

alexo

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;
}
 
V

Victor Bazarov

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
 
A

alexo

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.
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.
 
Ö

Öö Tiib

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.
 
A

alexo

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:)
 
B

Bart van Ingen Schenau

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
 
T

terminator

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.
 
T

Tobias Müller

terminator said:
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
 
A

alexo

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();
}
 
V

Victor Bazarov

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
 
A

alexo

Il 01/04/2013 14:55, Victor Bazarov ha scritto:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top