Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to structure classes involving friend function

Reply
Thread Tools

how to structure classes involving friend function

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      04-18-2010
The following post is for learning purpose only. I will try to avoid
friend functions and friend classes.

Consider the following piece of code:

class Test;

class Sample
{
public:
void friend_fn(const Test& ref);
// rest of the Sample class
};

class Test
{
public:
friend void Sample::friend_fn(const Test& ref);
// rest of the Test class
};

First approach:
We should structure the two class definitions only in the above
order(ie first the forward declaration of class Test followed by the
definition of class Sample and then the definition of class Test). Am
I correct ? If the two class definitions are kept in the above order
in the same header file, then there is no issue. But is it correct to
keep multiple class definitions in the same header file?

Now consider the following second approach:
Suppose I want to keep the definition of class Sample in, say,
Sample.h and the definition of class Test in Test.h. Also, suppose I
want to keep the function Sample::friend_fn() as an inline function.
Then the definition of Sample::friend_fn() should appear in Sample.h.
But the problem in this approach is that Sample::friend_fn() is going
to use some members of the class Test and we can only have a forward
declaration of class Test in Sample.h. My question: is this second
approach feasible ? If so, how to accomplish it ?

In real work environment, which approach is followed ? Kindly explain.

Thanks
V.Subramanian

 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      04-18-2010
On 18 apr, 18:12, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> Now consider the following second approach:
> Suppose I want to keep the definition of class Sample in, say,
> Sample.h and the definition of class Test in Test.h. Also, suppose I
> want to keep the function Sample::friend_fn() as an inline function.
> Then the definition of Sample::friend_fn() should appear in Sample.h.
> But the problem in this approach is that Sample::friend_fn() is going
> to use some members of the class Test and we can only have a forward
> declaration of class Test in Sample.h. My question: is this second
> approach feasible ? If so, how to accomplish it ?
>
> In real work environment, which approach is followed ? Kindly explain.


Something is wrong with your design. You tie two classes with close,
inline level friendship. The classes can not be kept or used
separately after that. Tie together and keep together:

/// Sample_and_Test.hpp
class Test;

class Sample
{
public:
inline void friend_fn(const Test& ref);
// rest of the Sample class

};

class Test
{
public:
friend void Sample::friend_fn(const Test& ref);
// rest of the Test class

};

inline void Sample::friend_fn(const Test& ref)
{
// do your intimate stuff with ref
}
 
Reply With Quote
 
 
 
 
Robert Fendt
Guest
Posts: n/a
 
      04-18-2010
And thus spake ", India" <>
Sun, 18 Apr 2010 08:12:17 -0700 (PDT):

> In real work environment, which approach is followed ? Kindly explain.


In the real world, you should probably avoid such 'design' at
all costs. Actually, it can never really work. If you use the
second class's interface in an inline function of the first
class, then the interface of the second class has to be known at
that point. So you have to include the complete class definition
beforehand.

Likewise, if the second class wants to use said inline function,
it has to know the complete class definition as well. This is a
'chicken or egg' kind of problem, and cannot be resolved without
some compromise.

The easiest way out would probably be to just use a saner design.
Class dependencies should form an acyclic graph (ideally a
tree). The acyclic property is crucial. If you have dependency
cycles like this, that's a sure sign there is something wrong
with your class structure.

If you can absolutely not avoid havinh dependency cycles (for
whatever reason), you have to use e.g. something like pointer to
implementation, which means you cannot keep everything inlined.

Regards,
Robert

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Must See Deifferences between Friend & Best Friend wonderfulinfo@gmail.com Digital Photography 17 08-07-2006 01:34 PM
Friend brings a friend. Shisha Girl MCSE 4 03-03-2006 02:42 AM
Friend brings a friend. Shisha Girl Python 0 03-02-2006 02:28 AM
Circular references involving internal classes Dave Rudolf C++ 2 03-05-2004 11:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57