Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In CLI/C++, can a derived class access protected members of based class?
The following code results in error C2248
C++
class B;
Class A{
  protected :
         B b;
         int m;
};
private class C : A
{
  public:
         void method();
}
C::method()
{
      A::m; //  error C2248 : m : cannot access protected members declared
in class A
   
}

How it can be made accessible?
Posted
Updated 13-Feb-11 21:05pm
v4
Comments
Sandeep Mewara 14-Feb-11 3:05am    
Next time, please use PRE tags to format code part.

As they are protected they are accessible in the derived class by definition of protected. Don't write "B->m", use just m.

By the way, your question is about CLI/C++, some language I am not familiar with. :) I know C++ and C++/CLI. You code is about C++.
Your code shows 9 errors, none of then is the one you describe!
Who are you trying to fool? I think, yourself. Be accurate!
If you don't believe it, try to compile it as is.

As your "code" contains a bug in almost every line, I cannot see a reason to fix it. "Unlearn what you have learned!"

—SA
 
Share this answer
 
v3
Comments
Nish Nishant 14-Feb-11 10:22am    
Voted 5.
Espen Harlinn 14-Feb-11 10:30am    
Good answer, my 5
As SA said, your code did not use any C++/CLI and what you pasted will not even compile. Here's the fixed version that will compile, although it has nothing to do with C++/CLI:

C++
class B{};

class A
{
protected :
  B b;
  int m;
};

private class C : A
{
public:
  void method();
};

void C::method()
{
  A::m;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900