Thread: base class protected members inaccessible

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    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
    28,413
    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)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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
    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
    3,445
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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, 10: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, 04:12 AM
  4. Virtual base class constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 02:18 AM
  5. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 12:43 AM