Protected vars not getting inherited?

This is a discussion on Protected vars not getting inherited? within the C++ Programming forums, part of the General Programming Boards category; I have Class A extending Class B and implementing Interface C. B accesses a protected var x from A. But ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    126

    Protected vars not getting inherited?

    I have Class A extending Class B and implementing Interface C. B accesses a protected var x from A. But the compiler is telling me that x is not declared in this scope. The code looks like

    Sender.h
    Code:
    #include "properties.h"
    class Sender
    {
        public:
            Sender(Properties p);
        protected
            int   sock;
            int   getSocket();
    };
    MyListener.h
    Code:
    #include "sender.h"
    class MyListener : public Sender, public Listener
    {
        public:
            void on_ready();
            int getSocket();
    };
    MyListener.cpp
    Code:
    #include "mylistener.h"
    int MyListener::getSocket()
    {
        return sock;
    }
    Compiling MyListener.cpp causes a "'sock' was not declared in this scope" error.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Except for a missing colon after protected, it looks okay to me. (Though you might want to provide your base classes with virtual destructors, and virtual inheritance might be appropriate.)
    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

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    126
    Well, I found the problem was one of those "see what you want to see" errors. I actually didn't have
    int MyListener::getSocket()
    but
    int getSocket()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. error C2248: can not access protected member
    By r0flc0pter in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2009, 10:34 AM
  3. stack vs. global vars : efficiency?
    By mynickmynick in forum C Programming
    Replies: 12
    Last Post: 07-31-2009, 02:39 PM
  4. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  5. Help With Inherited Classes Needed
    By elliott in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2005, 08:05 AM

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