Solved

inheriting friend

Posted on 1998-07-10
13
283 Views
Last Modified: 2010-04-01
class A
{    public :
      //
        friend    [ReturnType]       fn(..) {...}
...
}

class B : public A
{
  //
}

now the question-

will  fn(...)  be a friend of class B ?


i know " friendship is not inherited or transitive "
(i.e)   u may be my friend but i will not trust ur friends or kids.

here the question is  "can i trust my father's friends ?"
0
Comment
Question by:aby
  • 7
  • 5
13 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1167541
> will  fn(...)  be a friend of class B ?

No.

> here the question is  "can i trust my father's friends ?"

This is a different question ("*should* fn() be a friend of B)?") and depends on your specific context.
0
 

Author Comment

by:aby
ID: 1167542
thanx for the response

ref : answer from yonat

>here the question is  "can i trust my father's friends ?"    
>
>This is a different question ("*should* fn() be a friend of B)?") and depends on your
>specific context.

  can u explain  on this more? (of course u understand by' trusting father's friend ' i meant 'the friend function of parent being a friend of the subclass also' )

>'depends on the context' -
   more info please on the context part

whatz the case when doing Virtual Friend function idiom .please refer

http://www.cerfnet.com/~mpcline/On-Line-C++-FAQs/friends.html#[14.3]



0
 
LVL 22

Expert Comment

by:nietod
ID: 1167543
In the above example, fn is a friend of class A so it can access the private members of an A object.  Ussually this would be to access the private members of an A object that is passed in the parameters.  Since class B is derived from class A, fn() can also access the private members of the A part of an object of class B.  Thus fn(), in a sense, is also a friend of class B.  Now  it can't access private members of class B that are not part of class A.  

However, that doesn't usually matter.  Ussually a function is written with a particular class in mind and can handle classes derived from that particular class, but doesn't need to know anythign about the derived class.  Thus if additional private members were added to B it doesn't care, because it really only works with A's.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:aby
ID: 1167544
that cleared my doubts wonderfully.thanx.

i would greatly appreciate if anybody can comment on this.
 does it make any difference if
 a friend function is defined in public:/private?

also (ref :response from nietod)
>private members of the A part of an object of class B
does a B object do always contain all of A's members (be it private also) but only they cannot be accessed by the member functions of B.
and...does it differ in cases where B is derived private/protected/ B has a A

and please comment on the case of  Virtual Friend Function Idiom.please  ref.
http://www.cerfnet.com/~mpcline/On-Line-C++-FAQs/friends.html#[14.3]

0
 
LVL 22

Expert Comment

by:nietod
ID: 1167545
>> does it make any difference if a friend function is defined in public:/private?
Nope.

>>does a B object do always contain all of A's members (be it private also)
>>but only they cannot be accessed by the member functions of B. and...
>>does it differ in cases where B is derived private/protected/ B has a A

With data members (not member functions) A dirived class will always contain all of its base class's data members, whethor or not the members are public or private, and whethor or not the derivation was public or private.  A derived class IS the base class plus additional members (maybe-- there don't have to be additional ones but the base ones must be there.)

The way this is usually (always, really) implimented is that in memory an object of the the derived class begins with the exact same members as the base class, then it is followed in memory by any additianal members declared by the derived class.  Thus the derived class object has the same format in memory as the base class object, except it is followed by additional data.
0
 

Author Comment

by:aby
ID: 1167546
thanx nietod.that was great.

will appreciate if i can know where can i get the info on the like given on how an object is stored.

and can u coment on how it works in this. (Virtual Friend Function Idiom)

class Base {
    public:
      friend void f(Base& b);
      // ...
    protected:
      virtual void do_f();
      // ...
    };
   
    inline void f(Base& b)
    {
      b.do_f();
    }
   
    class Derived : public Base {
    public:
      // ...
    protected:
      virtual void do_f();  // "Override" the behavior of f(Base& b)
      // ...
    };
   
    void userCode(Base& b)
    {
      f(b);
    }

where i got this (url above)it's said like this.

The statement f(b) in userCode(Base&) will invoke b.do_f(), which is virtual. This means that Derived::do_f() will get control if b is actually a object of class Derived. Note that derived overrides the behavior of the protected: virtual member function do_f(); it does not have its own variation of the friend function, f(Base&).

OK now if the Derived: do_f() accesses the privete members of B (or is that the thing intended)and  why is "virtual" used in Derived? ( is it to allow it's children also to go in the same fashion)

0
 

Author Comment

by:aby
ID: 1167547
Adjusted points to 50
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167548
>> will appreciate if i can know where can i get the info on the like given on how an object is stored

Three sources that deal with it from time-to-time (not the focus) are BjarneSroustrup's "The C++ Programming Language"  and Scott Meyer's "Effective C++" and "More Effective C++" books.  These three books are manditory reading!  No programmer should be without them.  (They are not introductory level, they assume some knowledge of C++, but you are porabalby at or close to that point.)

I'm not 100% sure what your other question is asking about.  To avoid a lengthy explanation on topics you understand,

Do you know what a virtual function is?
Are you only asking why is the "virtual" keyword mentioned in the derived class's do_f()?
0
 

Author Comment

by:aby
ID: 1167549

>     Do you know what a virtual function is?
sure
>     Are you only asking why is the "virtual" keyword mentioned in the derived class's
     do_f()?

exactly.(as i understand "virtual" need be used in base class only)



0
 
LVL 22

Expert Comment

by:nietod
ID: 1167550
Right.  It only NEEDS to be.  But there is no harm in specifying it in a derived class (it makes no difference at all).  It is a good habit to do so, though, that way if someone looks at the definition of the derivided class they can know the function is virtual without having to look back at the base class.  

Note thatt once a function is virtual, it will always be virtual in all derived classes.  You can't "turn it off".
0
 

Author Comment

by:aby
ID: 1167551
thanx .that was great.the points are yours.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1167552
I'm answering for the moment.  But Yonat was here first and could have provided all the information I did (and then some)  I was just filling in her absence so you didn't have to wait.  You might want to consider giving her the points (the choice is yours), by rejecting my answer and waiting for her to submit one.
0
 

Author Comment

by:aby
ID: 1167553
sorry .had been out for quite some time
points are yours nietod.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question