Solved

inheriting friend

Posted on 1998-07-10
13
276 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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:aby
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:aby
Comment Utility
Adjusted points to 50
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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
Comment Utility

>     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
Comment Utility
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
Comment Utility
thanx .that was great.the points are yours.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
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
Comment Utility
sorry .had been out for quite some time
points are yours nietod.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now