Reputation: 22946
From http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5
A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes
So, what is the way to access the protected function fun
in the derived class?
#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 ();
}
};
This results in:
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 saw this: Accessing protected members in a derived class
Given:
You can only access protected members in instances of your type (or derived from your type). You cannot access protected members of an instance of a parent or cousin type.
In your case, the Derived class can only access the b member of a Derived instance, not of a different Base instance.
Changing the constructor to take a Derived instance will also solve the problem.
How can this be accomplished without changing the constructor declaration?
Upvotes: 5
Views: 38188
Reputation: 11
I know its a little late to address this issue, but my meagre understanding of inheritance, whilst being a beginner prompted me to answer it with an alternative approach. Corrections in my approach will be highly appreciated.
Since 'fun' is a protected member of class X, we can try and make a member function of same class X say 'void approachFun()', in the body or definition which we can call the 'fun' function. Make sure to keep this function "public" in class X. Now this funtion can be easily accessed by an object of class Y. Also the object of class Y to be made in main body of the code. You can refer to chatgpt to verify this method.
Note: Since this answer is also submitted as apart of my assignment and will be reviewed by my instructor[seeking your guidance in case this solution this wrong :) ]
Upvotes: 1
Reputation: 5342
In void Y::call ()
X objX;
objX.fun ();
// here you're trying to access the protected member of objX , this
/current object of Y
doesn't contains objX
as it's base object , they both are different objects. That is why you can't access its member.
you can make Y
friend of X
as a solution told by @anycom.
Edit: what I mean is, from inside Y
(which is inherited from X
) , you can simply call protected/public members of "it's" base class X
i.e you're accessing it's base members simply. But That doesn't means you can now access the protected members of all objects of type X
, since you're trying to access those members from outer scope of class X
i.e via object of X
. You know all these rules but it seem you did too much thinking 0_o
Upvotes: 1
Reputation: 104718
Well, if friend
is ok, then this angle may as well be ok:
#include <iostream>
class X {
private:
int var;
protected:
virtual void fun() {
var = 10;
std::cout << "\nFrom X" << var;
}
static void Fun(X& x) {
x.fun();
}
};
class Y : public X {
private:
int var;
public:
virtual void fun() {
var = 20;
std::cout << "\nFrom Y" << var;
}
void call() {
fun();
X objX;
objX.fun(); /* << ne-ne */
Fun(objX); /* << ok */
}
};
Of course, be mindful of the type you pass to X::Fun
if you use this as-is.
Upvotes: 1
Reputation: 4680
See this example:
#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(); /* call to Y::fun() */
X::fun (); /* call to X::fun() */
X objX;
/* this will not compile, because fun is protected in X
objX.fun (); */
}
};
int main(int argc, char ** argv) {
Y y;
y.call();
return 0;
}
This yields
From Y20 From X10
Because you have overloaded the fun()
-method in Y, you have to give the compiler a hint which one you mean if you want to call the fun
-method in X by calling X::fun()
.
Upvotes: 2
Reputation: 3277
I think that the thing you are trying to do should looks like this:
#include <iostream>
using namespace std;
class X
{
private:
int var;
protected:
virtual void fun ()
{
var = 10;
cout << "\nFrom X" << var;
}
};
class Y : public X
{
private:
int var;
public:
virtual void fun ()
{
var = 20;
cout << "\nFrom Y" << var;
}
void call ()
{
fun ();
X::fun ();
}
};
That way you can invoke hiden member from your base class. Otherwise you have to add friend X as it was pointed in other post.
Upvotes: 6
Reputation: 21819
You are not accessing protected function in your derived class, you are trying to overload it and promote from protected to public. This is a forbidden action, you only can hide functions in derived class, e.g. overload protected function as a private.
Accessing protected function means call it from some member of a class:
class Y : public X
{
public:
void call() {
fun();
}
}
or like you call it by objX.fun();
is also correct.
Upvotes: -1