public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/37112]  New: nested inherited class can't access protected members of base class
@ 2008-08-13 14:09 darkwingz at yahoo dot com
  2008-08-13 15:33 ` [Bug c++/37112] " chris dot fairles at gmail dot com
  2008-08-13 15:59 ` darkwingz at yahoo dot com
  0 siblings, 2 replies; 3+ messages in thread
From: darkwingz at yahoo dot com @ 2008-08-13 14:09 UTC (permalink / raw)
  To: gcc-bugs

The following code fails to compile:

class A {
protected:
    int i;
};

class B {
    class C : public A {
    public:
        void foo(A a) { 
            a.i = 1;
        }
    };
};

int main()
{

}

nest2.cpp: In member function 'void B::C::foo(A)':
nest2.cpp:3: error: 'int A::i' is protected
nest2.cpp:10: error: within this context

I can't find anything in the standard that says this should be invalid, but
perhaps I missed it. Thanks for your time and efforts.


-- 
           Summary: nested inherited class can't access protected members of
                    base class
           Product: gcc
           Version: 4.2.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: darkwingz at yahoo dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37112


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/37112] nested inherited class can't access protected members of base class
  2008-08-13 14:09 [Bug c++/37112] New: nested inherited class can't access protected members of base class darkwingz at yahoo dot com
@ 2008-08-13 15:33 ` chris dot fairles at gmail dot com
  2008-08-13 15:59 ` darkwingz at yahoo dot com
  1 sibling, 0 replies; 3+ messages in thread
From: chris dot fairles at gmail dot com @ 2008-08-13 15:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from chris dot fairles at gmail dot com  2008-08-13 15:32 -------
You're simply trying to access a protected member variable which is not
allowed. It doesn't matter if you try to access it from a member function of a
sub-object of A; if you have an object of type A, and it has a protected member
var i, you cannot access it directly. If foo() took a type C, or only tried to
access "i" (the protected member var of A from which C inherits) as opposed to
a.i, then it will compile as expected.

HTH,

Chris


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37112


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/37112] nested inherited class can't access protected members of base class
  2008-08-13 14:09 [Bug c++/37112] New: nested inherited class can't access protected members of base class darkwingz at yahoo dot com
  2008-08-13 15:33 ` [Bug c++/37112] " chris dot fairles at gmail dot com
@ 2008-08-13 15:59 ` darkwingz at yahoo dot com
  1 sibling, 0 replies; 3+ messages in thread
From: darkwingz at yahoo dot com @ 2008-08-13 15:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from darkwingz at yahoo dot com  2008-08-13 15:57 -------
Thanks for your reply. I think I would have seen this had I tried testing it
without the nesting. I tried to compile the following code:

class A {
protected:
    int i;
};

class B : public A {
public:
    void foo(A *a) {
        a->i = 1;
    }
};

And it also did not work. However, if I change the parameter of foo() to a B,
then it works. I guess this is to prevent a child class from modifying an
instance of type parent, and not type child. It's ok to modify something
protected if it's your type, but by making the parameter the type of the parent
you can't be sure it's the same child type. (Well, you can with dynamic_cast,
but it wouldn't make sense to allow it.) 

Thanks for the food for thought. I'll cancel the bug.


-- 

darkwingz at yahoo dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37112


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-08-13 15:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-13 14:09 [Bug c++/37112] New: nested inherited class can't access protected members of base class darkwingz at yahoo dot com
2008-08-13 15:33 ` [Bug c++/37112] " chris dot fairles at gmail dot com
2008-08-13 15:59 ` darkwingz at yahoo dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).