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


Groups > comp.lang.c++ > #11998 > unrolled thread

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

Started byOSS542 <jhart666@gmail.com>
First post2011-11-30 16:24 -0800
Last post2011-12-02 16:36 +0000
Articles 10 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#11998 — C++ puzzle - how to get this to compile ?

FromOSS542 <jhart666@gmail.com>
Date2011-11-30 16:24 -0800
SubjectC++ puzzle - how to get this to compile ?
Message-ID<1e5dd913-1403-42b9-bcc1-527ee7a31dbb@p2g2000vbj.googlegroups.com>
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();
};

[toc] | [next] | [standalone]


#12000

FromBart v Ingen Schenau <bart@ingen.ddns.info>
Date2011-12-01 13:54 +0100
Message-ID<jb7tel$ju1$1@localhost.localdomain>
In reply to#11998
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

[toc] | [prev] | [next] | [standalone]


#12105

FromOSS542 <jhart666@gmail.com>
Date2011-12-06 13:08 -0800
Message-ID<7db0eeff-4f4a-4f72-bb05-d9680e0241c5@m10g2000vbc.googlegroups.com>
In reply to#12000
My sincere thanks to Mr v Ingen Schenau and the rest of you for all
your very kind assistance and a most interesting discussion.

On Dec 1, 7:54 am, Bart v Ingen Schenau <b...@ingen.ddns.info> wrote:
>
> 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.
>

[toc] | [prev] | [next] | [standalone]


#12004

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2011-12-01 11:34 -0600
Message-ID<Xns9FAEC7120FD5Dmyfirstnameosapriee@216.196.109.131>
In reply to#11998
OSS542 <jhart666@gmail.com> wrote in news:1e5dd913-1403-42b9-bcc1-
527ee7a31dbb@p2g2000vbj.googlegroups.com:

> 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();
> };
> 

class cls2
{
    // This function may only be called from class cls1!
    void fun_for_cls1();
};
class cls1
{
        cls2 mbr1;
    public:
        void fun1() {
           mbr1.fun_for_cls1();
        }
    	
};

[toc] | [prev] | [next] | [standalone]


#12005

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-12-01 13:52 -0500
Message-ID<jb8icj$86t$1@dont-email.me>
In reply to#12004
On 12/1/2011 12:34 PM, Paavo Helde wrote:
> OSS542<jhart666@gmail.com>  wrote in news:1e5dd913-1403-42b9-bcc1-
> 527ee7a31dbb@p2g2000vbj.googlegroups.com:
>
>> 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();
>> };
>>
>
> class cls2
> {
>      // This function may only be called from class cls1!
>      void fun_for_cls1();
> };
> class cls1
> {
>          cls2 mbr1;
>      public:
>          void fun1() {
>             mbr1.fun_for_cls1();

'cls1::fun_for_cls1' is private in this context.

>          }
>      	
> };

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#12006

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-12-01 13:53 -0500
Message-ID<jb8iet$86t$2@dont-email.me>
In reply to#12005
On 12/1/2011 1:52 PM, Victor Bazarov wrote:
> On 12/1/2011 12:34 PM, Paavo Helde wrote:
>> OSS542<jhart666@gmail.com> wrote in news:1e5dd913-1403-42b9-bcc1-
>> 527ee7a31dbb@p2g2000vbj.googlegroups.com:
>>
>>> 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();
>>> };
>>>
>>
>> class cls2
>> {
>> // This function may only be called from class cls1!
>> void fun_for_cls1();
>> };
>> class cls1
>> {
>> cls2 mbr1;
>> public:
>> void fun1() {
>> mbr1.fun_for_cls1();
>
> 'cls1::fun_for_cls1' is private in this context.

Sorry, should be 'cls2::fun_for_cls1'

>
>> }
>>
>> };
>
> V


-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#12008

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2011-12-01 16:03 -0600
Message-ID<Xns9FAF9A3C1B9myfirstnameosapriee@216.196.109.131>
In reply to#12005
Victor Bazarov <v.bazarov@comcast.invalid> wrote in news:jb8icj$86t$1
@dont-email.me:

> On 12/1/2011 12:34 PM, Paavo Helde wrote:
>> OSS542<jhart666@gmail.com>  wrote in news:1e5dd913-1403-42b9-bcc1-
>> 527ee7a31dbb@p2g2000vbj.googlegroups.com:
>>
>>> 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();
>>> };
>>>
>>
>> class cls2
>> {
>>      // This function may only be called from class cls1!
>>      void fun_for_cls1();
>> };
>> class cls1
>> {
>>          cls2 mbr1;
>>      public:
>>          void fun1() {
>>             mbr1.fun_for_cls1();
> 
> 'cls1::fun_for_cls1' is private in this context.

Oops yes; I intended it to be public.

Cheers
Paavo
 

[toc] | [prev] | [next] | [standalone]


#12010

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-12-02 06:28 +0000
Message-ID<4ed8700f$0$4366$7b1e8fa0@news.nbl.fi>
In reply to#11998
OSS542 <jhart666@gmail.com> wrote:
> class cls2
> {
>    friend void cls1::fun1();
> };
> class cls1
> {
>        cls2 mbr1;
>    public:
>        void fun1();
> };

  In principle there's no technical reason why that couldn't be done. It's
just a question of lacking syntax in the language.

  It would be useful if one could, somehow, declare member functions before
the class definition is known. (If the class then fails to define the
declared member function, a linker error would happen.) This can be done
with namespaces, but not with classes.

  I'd say a syntax like this would be enough:

class A;
int A::foo(int, int);

  Anyone see any problem with that?

[toc] | [prev] | [next] | [standalone]


#12019

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-12-02 11:12 -0500
Message-ID<jbatda$li0$1@dont-email.me>
In reply to#12010
On 12/2/2011 1:28 AM, Juha Nieminen wrote:
> OSS542<jhart666@gmail.com>  wrote:
>> class cls2
>> {
>>     friend void cls1::fun1();
>> };
>> class cls1
>> {
>>         cls2 mbr1;
>>     public:
>>         void fun1();
>> };
>
>    In principle there's no technical reason why that couldn't be done. It's
> just a question of lacking syntax in the language.
>
>    It would be useful if one could, somehow, declare member functions before
> the class definition is known. (If the class then fails to define the
> declared member function, a linker error would happen.) This can be done
> with namespaces, but not with classes.
>
>    I'd say a syntax like this would be enough:
>
> class A;
> int A::foo(int, int);
>
>    Anyone see any problem with that?

I think you're advocating forward-declaration of member functions.  In 
that case the full qualification should be allowed for forward-declaring 
static, virtual, default, pure virtual and const members of the class. 
Basically there shouldn't be a problem if you want to write

     virtual int A::foo(int,int) const = 0;

outside the class itself.  However, currently the grammar does not allow 
the keyword 'virtual' to be used outside a class definition, and also 
defines a different meaning of the keyword 'static'.

I don't see "any problem" except that the grammar needs to be made even 
more complicated to allow out-of-the-class-definition member declarations.

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#12022

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-12-02 16:36 +0000
Message-ID<4ed8fea8$0$2782$7b1e8fa0@news.nbl.fi>
In reply to#12019
Victor Bazarov <v.bazarov@comcast.invalid> wrote:
> I think you're advocating forward-declaration of member functions.  In 
> that case the full qualification should be allowed for forward-declaring 
> static, virtual, default, pure virtual and const members of the class. 
> Basically there shouldn't be a problem if you want to write
> 
>     virtual int A::foo(int,int) const = 0;
> 
> outside the class itself.  However, currently the grammar does not allow 
> the keyword 'virtual' to be used outside a class definition, and also 
> defines a different meaning of the keyword 'static'.
> 
> I don't see "any problem" except that the grammar needs to be made even 
> more complicated to allow out-of-the-class-definition member declarations.

  Perhaps if there was a way to do a kind of "partial declaration" of a
class. In other words, you declare a class and some of its member functions
(and other stuff, such as member types and variables), but it's still not
a full class declaration (iow. you still can't instantiate the class
based solely on this partial declaration).

  I don't have a good idea for a syntax for this. I get the feeling that
the standardization committee abhors the addition of new reserved keywords
(in order to, I suppose, minimize name collisions with existing code), and
none of the existing reserved keywords that I can think of sound like a
good fit for this. Perhaps the least horrible keyword I can think of
would be 'namespace'. It could go something like this:

namespace class A // partial declaration of A
{
 public:
    virtual int foo(int, int) const = 0;
    int bar(int);
    int bar(int) const;
    class InnerClass;
};

  Now it would be possible to refer to those member functions and types of
the class A (and even call them using a reference/pointer of type A) before
we have a full definition.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web