 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
pocmatos@gmail.com Guest
|
Posted: Mon Nov 28, 2005 9:41 am Post subject: Const Friend? |
|
|
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
|
Posted: Mon Nov 28, 2005 10:34 am Post subject: Re: Const Friend? |
|
|
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
|
Posted: Mon Nov 28, 2005 2:11 pm Post subject: Re: Const Friend? |
|
|
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: ; }
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: () and B::y() to access A: and A::y.
};
--
Neil Cerutti
|
|
| Back to top |
|
 |
|
|
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
|
|