Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12000

Re: C++ puzzle - how to get this to compile ?

From Bart v Ingen Schenau <bart@ingen.ddns.info>
Newsgroups comp.lang.c++
Subject Re: C++ puzzle - how to get this to compile ?
Date 2011-12-01 13:54 +0100
Organization No organisation in particular
Message-ID <jb7tel$ju1$1@localhost.localdomain> (permalink)
References <1e5dd913-1403-42b9-bcc1-527ee7a31dbb@p2g2000vbj.googlegroups.com>

Show all headers | View raw


OSS542 Wrote:

> How might one go about getting something like the following
> example to compile successfully ?
> 
> class cls2
> {
>     friend void cls1::fun1();
> };
> class cls1
> {
>         cls2 mbr1;
>    public:
>         void fun1();
> };

As is, you can't.
When processing cls2, the compiler does not yet know there is a cls1 with a
member function fun1, so the friend declaration is ill-formed.
And you can not move the definition of cls1 to before cl2 due to the member
object mbr1.

The following rewrites are possible to make it compile:
<code 1>
// Make the entire cls1 class a friend of cls2
class cls1;
class cls2
{
    friend class cls1;
};
class cls1
{
        cls2 mbr1;
   public:
        void fun1();
};
</code 1>

<code 2>
// Use a pointer for the member in cls1
class cls2;
class cls1
{
        cls2* mbr1;
   public:
        void fun1();
};
class cls2
{
    friend void cls1::fun1();
};
</code 2>

Bart v Ingen Schenau

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C++ puzzle - how to get this to compile ? OSS542 <jhart666@gmail.com> - 2011-11-30 16:24 -0800
  Re: C++ puzzle - how to get this to compile ? Bart v Ingen Schenau <bart@ingen.ddns.info> - 2011-12-01 13:54 +0100
    Re: C++ puzzle - how to get this to compile ? OSS542 <jhart666@gmail.com> - 2011-12-06 13:08 -0800
  Re: C++ puzzle - how to get this to compile ? Paavo Helde <myfirstname@osa.pri.ee> - 2011-12-01 11:34 -0600
    Re: C++ puzzle - how to get this to compile ? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-01 13:52 -0500
      Re: C++ puzzle - how to get this to compile ? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-01 13:53 -0500
      Re: C++ puzzle - how to get this to compile ? Paavo Helde <myfirstname@osa.pri.ee> - 2011-12-01 16:03 -0600
  Re: C++ puzzle - how to get this to compile ? Juha Nieminen <nospam@thanks.invalid> - 2011-12-02 06:28 +0000
    Re: C++ puzzle - how to get this to compile ? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-02 11:12 -0500
      Re: C++ puzzle - how to get this to compile ? Juha Nieminen <nospam@thanks.invalid> - 2011-12-02 16:36 +0000

csiph-web