LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Why can't we access a protected member in a derived class by a base class's object? (http://www.linuxquestions.org/questions/programming-9/why-cant-we-access-a-protected-member-in-a-derived-class-by-a-base-classs-object-927539/)

TheIndependentAquarius 02-04-2012 05:21 AM

Why can't we access a protected member in a derived class by a base class's object?
 
Code:

#include <iostream>
using namespace std;

class X
{
    private:
        int var;
    protected:
        void fun ()
        {
            var = 10;
            cout << "\nFrom X" << var;
        }
};

class Y : public X
{
    private:
        int var;
    public:
        void fun ()
        {
            var = 20;
            cout << "\nFrom Y" << var;
        }

        void call ()
        {
            fun ();

            X objX;
            objX.fun ();

        }
};

Results in:
Code:

anisha@linux-dopx:~/> g++ type.cpp
type.cpp: In member function ‘void Y::call()’:
type.cpp:9:8: error: ‘void X::fun()’ is protected
type.cpp:32:14: error: within this context

I request the "reasons".

Redrobes 02-04-2012 08:10 AM

The interface to X's fun is protected. Therefore you can call it from within a function declared as part of class X or you can call it directly from a class derived from X. What you cant do is have a function of Y and declare a local variable of type X and then call its function. The local copy is not one derived from Y and the function of X is not public to call.

I.e. You can only do:
X::Func() { fun() }
Y::Func() { fun() }

but not:
Y:Func() { X x; x.fun() }

dwhitney67 02-04-2012 12:43 PM

And if you want to call X::fun() from within a function of Y, then something like this could be used:
Code:

class Y : public X
{
    ...

    void calc()
    {
        fun();      // calls Y::fun()

        X::fun();    // calls X::fun()... duh
    }
};


rainbowsally 02-04-2012 04:49 PM

I think you can also declare Y a "friend" class for X.

Stuck in windows at the moment. Can't send a code example.

dwhitney67 02-04-2012 09:12 PM

Quote:

Originally Posted by rainbowsally (Post 4593933)
I think you can also declare Y a "friend" class for X.

Stuck in windows at the moment. Can't send a code example.

<statement deleted.>

rainbowsally 02-04-2012 10:35 PM

You can add
Code:

friend class Y;
in the body of the X class declaration near the top.

You can also un-protect everything in a class header you #include like this:
Code:

#define protected public
#include "X.h"
#undef protected

but that header must be the first header #included or you'll unprotect everything that loads while this new definition is effective.

The above is only for classes you can't easily rewrite or for quick testing your concepts.

Another approach would be to make a friend class that doesn't exist so that it can be #defined when the header loads.

Something like this.
Code:

class X
{
friend class X_accessor;
...
};

Then instead of changing the definition of 'protected' change the definition of X_accessor in the file that declares Y.
Code:

#define X_accessor Y
#include "X.h"
#undef X_accessor



All times are GMT -5. The time now is 03:49 AM.