Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C/C++ query(nested struct)

5 views
Skip to first unread message

Abhishek

unread,
Aug 6, 2010, 6:48:35 AM8/6/10
to
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

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jeremy

unread,
Aug 7, 2010, 6:20:55 AM8/7/10
to
On Aug 6, 5:48 am, Abhishek <goluagarw...@gmail.com> wrote:
> In case of a nested struct, does the inner struct can access the
> private member of outer struct.


I think originally it was an error, but there was a defect raised to
actually make it legal:

http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#45

CornedBee

unread,
Aug 7, 2010, 9:30:50 PM8/7/10
to
On Aug 6, 3:48 am, Abhishek <goluagarw...@gmail.com> wrote:
> 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
>
> }

This was indeed an error in C++03, but the restriction was later
considered to be a bad idea and lifted.
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#45
Since it's a defect, GCC probably decided to retroactively fix it even
for its C++03 mode.

Sebastian

0 new messages