Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > friend function accessing protected members of base class

Reply
Thread Tools

friend function accessing protected members of base class

 
 
Rahul
Guest
Posts: n/a
 
      04-07-2008
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 ! ! !
 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      04-07-2008
On Apr 7, 5:56 pm, Rahul <(E-Mail Removed)> wrote:
> 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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing derived class members from base class Bhawna C++ 7 08-26-2008 11:03 AM
Static Method in base class accessing protected constructor of derived clas softwaredoug@gmail.com C++ 4 10-23-2007 02:32 PM
accessing base class members when base is template flopbucket C++ 2 06-23-2006 01:40 AM
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
accessing protected data members of instance of parent class Suzanne Vogel C++ 5 12-09-2003 01:55 AM



Advertisments