473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Nov 28 '05 #1
2 2711
On 28 Nov 2005 01:41:02 -0800, po******@gmail.com wrote:
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 */}
};

Nov 28 '05 #2
On 2005-11-28, po******@gmail.com <po******@gmail.com> wrote:
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::x; }
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::x() and B::y() to access A::x and A::y.
};

--
Neil Cerutti
Nov 28 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Matthew Monopole | last post by:
hi: When we declare something like: func(const class a) we protect the variable a from being modified during func...right? okay first question: if a is passed by value, who care if it's...
6
by: AlesD | last post by:
Hello, I can't figure out how to build assignment operator for class which contains "type* const value". --- example --- class parent_t; class child_t {
6
by: Senthilvel | last post by:
Hi folks, My friend tells that the following function declaration is illegal. void Foo(const string& strData = "Default"); My friend argues that it is not legal to provide a default value for...
7
by: ±ÇÌéÍõ×Ó | last post by:
Hi,guys! I find an example from the book "Advanced C++ Programming Styles and idoms" by James O.Coplien, but it fails to compile. Code: class String{ public: friend String operator+ (const...
2
by: TonyM | last post by:
Q: Is there a good way to overcome this apparent bug without modifying the mfc code? ___________________________________ Info: Although it is NOT a good idea, I seemed to have perhaps located a...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
26
by: Turin | last post by:
Dear all; As far as I understand the idea behind getter methods, it is used to make sure that private memers of a class is returned appropriately to the calling object. However, if all I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.