C/C++ query(nested struct)

0 views
Skip to first unread message

Abhishek

unread,
Aug 6, 2010, 11:12:20 PM8/6/10
to DS & Algo@itbhu
In case of a nested struct, does the inner struct can access the
private member of outer struct.

struct str1
{
private:
int a;
public:
struct str2
{
public:
void func(void);
};

};

void str1::str2::func()
{
str1 obj;
obj.a=10; //this is working in GNU compiler, but according to
Bruce Eckel this should be an error

}

int main()
{
str1::str2 obj2;
obj2.func();

}

Kindly solve my doubt, this really is reported to be an error in Bruce
Eckel volume 1 but when i run this on my GNU C++ compiler this gave me
output.

Regards,
Abhishek

prashant bhutani

unread,
Aug 7, 2010, 5:53:30 AM8/7/10
to ds--al...@googlegroups.com
Well I am not sure about it. But I think that when you are decalring
str1::str2 obj2
then the memory is allocated for a structure member (structure in this case) and when you are calling the obj2.func() which declares str1 object and defines values of a, then the compiler due to all the happenings is thinking str2 as a friend member of structure so it allows you to alter value of a.

Well this is a vague idea. If anyone knows answer to this please enlighten us all.

Regards,
Prashant Bhutani
Junior Undergraduate Student
CSE, IT-BHU


--
You received this message because you are subscribed to the Google Groups "DS & Algo@itbhu" group.
To post to this group, send an email to ds--al...@googlegroups.com.
To unsubscribe from this group, send email to ds--algoitbh...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ds--algoitbhu?hl=en-GB.


Reply all
Reply to author
Forward
0 new messages