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 

Const Friend?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
pocmatos@gmail.com
Guest





PostPosted: Mon Nov 28, 2005 9:41 am    Post subject: Const Friend? Reply with quote



Hi all,

Is it possible to declare a const friend or something. I wish to make
class B friend of class A but I don't want it to be changing A. It's a
read-only friend. Is this possible?

Thanks,

Paulo Matos

Back to top
Zara
Guest





PostPosted: Mon Nov 28, 2005 10:34 am    Post subject: Re: Const Friend? Reply with quote



On 28 Nov 2005 01:41:02 -0800, [email]pocmatos (AT) gmail (DOT) com[/email] wrote:

Quote:
Hi all,

Is it possible to declare a const friend or something. I wish to make
class B friend of class A but I don't want it to be changing A. It's a
read-only friend. Is this possible?

Thanks,

Paulo Matos


class A {
/*...*/
friend class B;
/*...*/
};

class B {
/*...*/
void function(const class A& what)
{
/* this is the function that accesses A, only through what reference
and not using const_cast */}
};


Back to top
Neil Cerutti
Guest





PostPosted: Mon Nov 28, 2005 2:11 pm    Post subject: Re: Const Friend? Reply with quote



On 2005-11-28, [email]pocmatos (AT) gmail (DOT) com[/email] <pocmatos (AT) gmail (DOT) com> wrote:
Quote:
Is it possible to declare a const friend or something. I wish
to make class B friend of class A but I don't want it to be
changing A. It's a read-only friend. Is this possible?

Include an in-between friend that provides the access for the
members you want to share.

class A {
int x, y;
friend class B;
};

class B {
const int &x() const { return A:Mad; }
const int &y() const { return A::y; }
/* If they're really built-in types, you wouldn't necessarily use
references. But in general, you would. */
friend class C;
};

class C {
// Must use B:Mad() and B::y() to access A:Mad and A::y.
};

--
Neil Cerutti

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.