Topic: A Question about friend specification


Author: sreeni@bhaskara.cs.albany.edu (Sreenivasa Rao Viswanadha)
Date: 1995/06/13
Raw View
Hi,

I have a question regarding friend declarations. Does adding a friend decl. to
a class change its member access, exceptions etc.? More specificly, consider
the following :

File : t1.hh

class X
{
  int i;
};

File : t2.hh

class X
{
   int i;
   friend class Y;
};

Now, if I have file t1.cc that include t1.hh and file t2.cc that includes file
t2.hh, when I compile and link t1.cc t2.cc, will there be any problems.

Also, does the presence of exceptions (or if X was used for exception handling)
have any affect on inclusion of friend?

I could not find any specific answer in the ref. manual.

Thanks for any help,
Sreeni.





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/13
Raw View
In article kj5@rebecca.albany.edu, sreeni@bhaskara.cs.albany.edu (Sreenivasa Rao Viswanadha) writes:
>Hi,
>
>I have a question regarding friend declarations. Does adding a friend decl. to
>a class change its member access, exceptions etc.?

No, or probably no, depending on what you include in 'etc'.

>More specificly, consider the following :
>
>File : t1.hh
>
>class X
>{
>  int i;
>};
>
>File : t2.hh
>
>class X
>{
>   int i;
>   friend class Y;
>};
>
>Now, if I have file t1.cc that include t1.hh and file t2.cc that includes file
>t2.hh, when I compile and link t1.cc t2.cc, will there be any problems.

That is a completely different question. Now you are asking whether you
can use two different versions of a class declaration in the same program.
The results of doing so are undefined, so don't do it.

This specific case will probably work, but you can't depend on it. There
is room for some subtle interactions, depending on what names are
visible in what scopes.

>Also, does the presence of exceptions (or if X was used for exception handling)
>have any affect on inclusion of friend?

I don't understand this question. If it means you want to use different
versions of a class definition in different parts of your program, don't do it.

---
Steve Clamage, stephen.clamage@eng.sun.com







Author: ellis@allegra.att.com (Margaret Ellis)
Date: 1995/06/14
Raw View
In article <3rkq0h$kj5@rebecca.albany.edu> sreeni@bhaskara.cs.albany.edu (Sreenivasa Rao Viswanadha) writes:
>Hi,
>
>I have a question regarding friend declarations. Does adding a friend decl. to
>a class change its member access, exceptions etc.? More specificly, consider
>the following :
>
>File : t1.hh
>
>class X
>{
>  int i;
>};
>
>File : t2.hh
>
>class X
>{
>   int i;
>   friend class Y;
>};
>
>Now, if I have file t1.cc that include t1.hh and file t2.cc that includes file
>t2.hh, when I compile and link t1.cc t2.cc, will there be any problems.
>
>Also, does the presence of exceptions (or if X was used for exception handling)
>have any affect on inclusion of friend?
>
>I could not find any specific answer in the ref. manual.
>
>Thanks for any help,
>Sreeni.


The effects of defining your class X differently in two different places
in the same program are undefined.  Adding a friend declaration to a class
does not change its access protections (other than granting access to the
new friend); nor does adding a friend affect what exceptions are thrown
by any of the class's member functions.  If fact, while the code you
show is undefined, you will be able to get away with it on most current
compilations systems.  I know of no compilation system that enforces
class declarations matching between translation units.

You might be interested in reading the section on link compatibility in
"Designing and Coding Reusable C++" (by Martin Carroll & Margaret Ellis,
Addison-Wesley, 1995).  We discuss the kinds of changes you can and
can't get away with without having to recompile all your source files.
(We don't recommend doing everything we tell you you can get away with. :-) )


Margaret A. Ellis