Question:
why can't I access protected member of base class? c++?
raskalemano
2011-02-09 18:34:03 UTC
I'm trying to compile somehing like the following

class Base
{
protected:
int protected_num;
....}

class X : private Base
{...
public:
void function ()
{ cout << Base.protected_num(); }


I get "error C2248 cannot access private member declared in class"
Five answers:
Ratchetr
2011-02-09 18:51:18 UTC
private Base is fine.



But protected_num isn't a function. So it shouldn't be followed by ().



And Base.protected_num is wrong. You don't really need to qualify protected_num, unless you also had an instance in X.



You can simply do this in X:

cout << protected_num;



If you did need to qualify it:

cout << Base::protected_num;
tfloto
2011-02-09 18:47:42 UTC
Two things

1. You need not access protected_num through Base - it's an inherited member.

2. protected_num is not a function so here is what you're statement should look like



cout << protected_num;



class X : private Base is fine. the class is private only within the confines of class X. anything trying to access Base through class X will get the private member error. same with anything trying to derive for class X.
smart
2016-10-06 13:06:30 UTC
use "secure" modifier whilst inheriting... inner maximum individuals of the backside class won't be able to be inherited. If u use secure modifier whilst inheriting all public individuals will enhance into secure individuals of the derived class. Public individuals might properly be accessed by employing each and all the instructions. yet secure individuals of a class can in straightforward terms be get admission to by employing the instructions which derive from it.
2011-02-09 19:13:15 UTC
class Base

{

protected:

int protected_num;

....}



class X : private Base

{...

public:

void function ()

{ cout << Base.protected_num(); } // change this to cout<
Myke
2011-02-09 18:41:45 UTC
change your code from "class X : private Base" to "class X : public Base" and it will work. The reason it doesn't work is because you specified private inheritance from your base class.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...