C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Who needs access to copy nested class?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Maciej Sobczak
Guest





PostPosted: Thu Jan 27, 2005 9:24 pm    Post subject: Who needs access to copy nested class? Reply with quote



Hi,

Consider the following:

class A
{
class B
{
B() {}
B(B const &) {} // line 6.

friend class A;
};

public:
A() {}
A(B const &) {} // line 13.

B getB() { return B(); }
};

int main()
{
A a1;
A a2(a1.getB()); // line 21.
}

The idea is to have object of class A initialized with some "secret
messenger" of type A::B, got from existing instance of A.
In particular, in line 21. object a2 is initialized with the temporary
"secret messenger" of type A::B, returned by a1.getB().

I thought that it is enough if A::B and A make friends.

Some compilers are happy with it, but g++ (3.4.2) thinks different:

$ g++ -Wall -pedantic t.cc
t.cc: In function `int main()':
t.cc:6: error: `A::B::B(const A::B&)' is private
t.cc:21: error: within this context
$

Of course, adding friendship between A::B and main() (not counting that
the name "main" should not be used this way) makes it work with this
compiler.

It appears that g++ expects that main() should be able to copy A::B, as
if it was its own business.
For other compilers, passing the instance of A::B between two instances
of A does happen on the main() territory, but in a way that main() does
not care about it.

It's like passing a secret document between two official embassies, on
another country's territory. :)

Who is right?

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Antoun Kanawati
Guest





PostPosted: Fri Jan 28, 2005 11:26 am    Post subject: Re: Who needs access to copy nested class? Reply with quote



Maciej Sobczak wrote:
Quote:
Hi,

Consider the following:

class A
{
class B
{
B() {}
B(B const &) {} // line 6.

friend class A;
};

public:
A() {}
A(B const &) {} // line 13.

B getB() { return B(); }
};

int main()
{
A a1;
A a2(a1.getB()); // line 21.
}

The idea is to have object of class A initialized with some "secret
messenger" of type A::B, got from existing instance of A.
In particular, in line 21. object a2 is initialized with the temporary
"secret messenger" of type A::B, returned by a1.getB().

I thought that it is enough if A::B and A make friends.

Some compilers are happy with it, but g++ (3.4.2) thinks different:

$ g++ -Wall -pedantic t.cc
t.cc: In function `int main()':
t.cc:6: error: `A::B::B(const A::B&)' is private
t.cc:21: error: within this context
$
[snip]
It appears that g++ expects that main() should be able to copy A::B, as
if it was its own business.

It is its business. There is an anonymous temporary variable that holds
the copy of A::B created in main(). The code is roughly equivalent to:

int main()
{
A a1;
A::B temp(a1.getB());
A a2(temp); // line 21.
}

Quote:
For other compilers, passing the instance of A::B between two instances
of A does happen on the main() territory, but in a way that main() does
not care about it.

It's like passing a secret document between two official embassies, on
another country's territory. Smile

That's reasoning by analogy; but the analogy does not hold. This is
more like copying a secret document at Kinko's across the street from
the embassy, in the third country's territory, and then passing that
copy to the other second embassy. This will require that you hand the
document over to the clerk to run it through the copier, but since the
document is copy-protected, the procedure will fail; alternately, the
document is copyable, and the clerk can make an unauthorized copy.

--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
White Wolf
Guest





PostPosted: Fri Jan 28, 2005 11:29 am    Post subject: Re: Who needs access to copy nested class? Reply with quote



Maciej Sobczak wrote:
Quote:
Hi,

Consider the following:

class A
{
class B
{
B() {}
B(B const &) {} // line 6.

friend class A;
};

public:
A() {}
A(B const &) {} // line 13.

B getB() { return B(); }
};

int main()
{
A a1;
A a2(a1.getB()); // line 21.
}
[SNIP]
It's like passing a secret document between two official embassies, on
another country's territory. :)

Who is right?

I must say gcc. Why? Because EDG says it:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 21: error: "A::B::B(const A::B &)" is inaccessible
(Even though the copy was eliminated, the standard
still requires it to be accessible)
A a2(a1.getB()); // line 21.
^

1 error detected in the compilation of "ComeauTest.c".

BTW, Comeau (Computing) is your friend:

http://www.comeaucomputing.com/tryitout/

;-)

BTW for a possible solution: just think about the two official embassies
passing a secret document. Do they have an invisible attache case or do
they have one which only the recipient can open and understand? ;-)

--
WW aka Attila
:::
Princess Leia: @(-_-)@



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.