473,725 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to access the outer structure member variable from inner struture?

Hi.

Are there any way to access the integer member 'a' of outer structure
from
inner structure's member function func_inner()?
See below the structure...

Thanks.

struct outer {
int a;
struct inner {
int b;
void func_inner(void )
{
cout << "Inner func" << endl;
cout << "inner b: " << b << endl;
//cout << "outer a: " << outer::a << endl;
//cout << "outer a: " << a << endl;
}
} i;
void func_outer(void )
{
cout << "Outer func" << endl;
cout << "inner b: " << i.b << endl;
cout << "outer a: " << a << endl;
}
};

May 4 '06 #1
2 2937

"dobest03" <do******@empal .com> schrieb im Newsbeitrag
news:11******** *************@g 10g2000cwb.goog legroups.com...
Hi.

Are there any way to access the integer member 'a' of outer
structure
from
inner structure's member function func_inner()?
See below the structure...

Thanks.

struct outer {
int a;
// make inner a friend of outer
struct inner;
friend class inner;
struct inner { // have a poitner to outer
outer& rOuter;
// no default c'tor: have to fill reference
inner(outer& out) {rOuter&out;}

int b;
void func_inner(void )
{
cout << "Inner func" << endl;
cout << "inner b: " << b << endl;
cout << "outer a: " << rOuter.a << endl;
}
} i;
void func_outer(void )
{
cout << "Outer func" << endl;
cout << "inner b: " << i.b << endl;
cout << "outer a: " << a << endl;
}
// constructor must initialize 'i'
outer(): i(this) {} };

HTH,
-Gernot

May 4 '06 #2
Gernot Frisch wrote:
"dobest03" <do******@empal .com> schrieb im Newsbeitrag
news:11******** *************@g 10g2000cwb.goog legroups.com...
Hi.

Are there any way to access the integer member 'a' of outer
structure
from
inner structure's member function func_inner()?
See below the structure...

Thanks.

struct outer {
int a;
// make inner a friend of outer
struct inner;
friend class inner;


There's really no need for this since the default access specifier for
members of a struct is public.
struct inner { // have a poitner to outer
outer& rOuter;
// no default c'tor: have to fill reference
inner(outer& out) {rOuter&out;}


This is the right idea but a reference has to be seated in the ctor
initializer list, not in the ctor body.

inner (outer& out) : rOuter(out) {}

In any event, to state explicitly what your solution implies: an
instance of a nested class is not automatically associated with an
instance of the outer class-- indeed one can instantiate the inner class
without ever instantiating the outer class. (If memory serves, this is
in contrast to non-static Java inner classes which are always associated
with an instance of the outer class.)

int b;
void func_inner(void )
{
cout << "Inner func" << endl;
cout << "inner b: " << b << endl;
cout << "outer a: " << rOuter.a << endl;
}
} i;
void func_outer(void )
{
cout << "Outer func" << endl;
cout << "inner b: " << i.b << endl;
cout << "outer a: " << a << endl;
}


// constructor must initialize 'i'
outer(): i(this) {}


Almost... should actually be:
outer() : i(*this) {}
};



-Mark
May 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9760
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a reduced code example: class List { private: void *rootNode; class Iterator {
9
1538
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
5
2428
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include <iostream> class Outer { private: int outer_data;
166
8634
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
4639
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method or field public? Thanks, Dennis
6
1669
by: MilanB | last post by:
Hello //----------------- I have following situation: public class Class1 { public struct Struct1 { public void Function1() {
1
1543
by: Edward C. Jones | last post by:
#! /usr/bin/env python """ When I run the following program I get the error message: UnboundLocalError: local variable 'x' referenced before assignment Can "inner" change the value of a variable defined in "outer"? Where is this explained in the docs? """ def outer():
2
1621
by: Neil | last post by:
I am trying to access the public methods of a private data member in the composition. The following is my code. Am I right? If the return value of function in class Outer 'getInner()' is not reference, I cannot change the data member ia, why? //outer.h class Outer { class Inner { int ia; public:
9
4688
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
8874
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9092
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6000
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3212
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.