base class protected members inaccessible

This is a discussion on base class protected members inaccessible within the C++ Programming forums, part of the General Programming Boards category; I'm having an issue that I don't understand. as I understand the C++ standard, my code should work the way ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590

    base class protected members inaccessible

    I'm having an issue that I don't understand. as I understand the C++ standard, my code should work the way I have it.

    Code:
    class A
    {
      protected:
        int i;
    };
    
    class B : public A
    {
      public:
        int foo()
        {
          return i;
        }
    };
    gcc 4.2.1 on opensuse 10.3 i386 complains saying that A::i is private in the context of the drived class. I don't get it. I thought protected members were accessible to derived classes. what am I doing wrong here?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    gcc 4.3.2 compiles that without errors.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    You are right, but it is unlikely that you have found a compiler bug in a language feature that is not exactly rarely used. Is this the entire program (e.g., we add an empty global main function and compile to an executable program and you get that error)?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It looks right, and g++ 4.4.1 accepts it here. I spent a little time looking at bug reports in GCC but didn't find anything that looked like it matches. Still....

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    No complains from 4.4.1 under MinGW in Win7 x64.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    ok, so now I added default constructors to both classes, and it compiles. I really don't understand now.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be if you posted real code?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    The only catch to protected members is that the rule applies only to instances of your own object. In other words you cannot pass in a pointer to another instance of the class and access those protected members from your instance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot declare inherited class of virtual Base
    By rogster001 in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2009, 10:25 AM
  2. Access to members of protected base class
    By DL1 in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2009, 11:30 AM
  3. Singleton base template class? How do you do that?!
    By arcaine01 in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2009, 05:12 AM
  4. Virtual base class constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 03:18 AM
  5. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 01:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21