LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2012, 05:21 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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".
 
Old 02-04-2012, 08:10 AM   #2
Redrobes
LQ Newbie
 
Registered: Feb 2012
Posts: 9

Rep: Reputation: Disabled
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() }
 
Old 02-04-2012, 12:43 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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
    }
};
 
Old 02-04-2012, 04:49 PM   #4
rainbowsally
Member
 
Registered: Oct 2011
Posts: 47
Blog Entries: 138

Rep: Reputation: Disabled
I think you can also declare Y a "friend" class for X.

Stuck in windows at the moment. Can't send a code example.
 
Old 02-04-2012, 09:12 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by rainbowsally View Post
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.>

Last edited by dwhitney67; 02-05-2012 at 06:28 AM. Reason: poor choice of words
 
Old 02-04-2012, 10:35 PM   #6
rainbowsally
Member
 
Registered: Oct 2011
Posts: 47
Blog Entries: 138

Rep: Reputation: Disabled
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

Last edited by rainbowsally; 02-05-2012 at 06:53 AM. Reason: incomplete example
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Access inherited member from base class? (C++) cbh2000 Programming 8 09-16-2010 11:06 AM
[SOLVED] Python n00b: promoting a derived class to base class? CoderMan Programming 2 03-11-2010 01:46 PM
HELP:: Can I have virtual functions in derived class?? < Are they restricted to Base> caruna Programming 3 12-22-2009 10:17 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration