friend function accessing protected members of base class

R

Rahul

Hi Everyone,

I have the following code,

class sample
{
protected : int i;
private: int j;
public: sample()
{
i = 5;
j = 10;
}
};

class two : public sample
{
public: two()
{
i = 10;
}
friend void test()
{
i = 111;
}
};

And i get an error saying test() cannot access two::sample::i;
Is there any was for the friend function test() to access the
protected members?

Thanks in advance ! ! !
 
R

Rahul

Hi Everyone,

I have the following code,

class sample
{
protected : int i;
private: int j;
public: sample()
{
i = 5;
j = 10;
}
};

class two : public sample
{
public: two()
{
i = 10;
}
friend void test()
{
i = 111;
}
};

And i get an error saying test() cannot access two::sample::i;
Is there any was for the friend function test() to access the
protected members?

Thanks in advance ! ! !

oops, not doing the write way, found the mistake

friend void test()
{
two obj;
obj.i = 111;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top