473,405 Members | 2,160 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,405 software developers and data experts.

C++ References

I want bi to be changed each time ai changes ,possibly using
references.Can anyone help me : bellow is the code - please suggest
changes -

#include<iostream>
using namespace std;
class B
{
private:
int bi;
public:
B()
{
bi=10;
}
int& get_bi()
{
return bi;
}
void display()
{
cout<<"bi = "<<bi<<endl;
}
};
class A
{
private:
B b;
int ai;
public:
A()
{
ai=b.get_bi();
}
void assign(int i)
{
ai=i;
}
void display()
{
cout<<"ai = "<<ai<<endl;
}
};
int main()
{
B bo;
bo.display();

A ao;
ao.display();
ao.assign(1111);
ao.display();
bo.display();

return 0;
}

May 10 '06 #1
5 1507
onkar wrote:
I want bi to be changed each time ai changes ,possibly using
references.Can anyone help me : bellow is the code - please suggest
changes -
Why do you have ai in the first place? Your A contains a B and can read bi's
value with get_bi(), so there is no need to have another variable that you
would have to keep consistent with it. You could make ai a reference, but I
don't really see an advantage in that.
#include<iostream>
using namespace std;
class B
{
private:
int bi;
public:
B()
{
bi=10;
}
int& get_bi()
{
return bi;
}
The above function isn't very useful. Instead, you can simply make bi
public. The idea of accessor functions is that you can replace the internal
represenation without changing the interface, but that requires separate
get/set functions. With your verison, the representation can't be changed
anyway, so you can as well expose the member variable directly.
void display()
{
cout<<"bi = "<<bi<<endl;
}
};
class A
{
private:
B b;
int ai;
public:
A()
{
ai=b.get_bi();
}
void assign(int i)
{
ai=i;
}
void display()
{
cout<<"ai = "<<ai<<endl;
}
};
int main()
{
B bo;
bo.display();

A ao;
ao.display();
ao.assign(1111);
ao.display();
bo.display();

return 0;
}


May 10 '06 #2
onkar wrote:
I want bi to be changed each time ai changes ,possibly using
references.Can anyone help me : bellow is the code - please suggest
changes -

#include<iostream>
using namespace std;
class B
{
private:
int bi;
public:
B()
{
bi=10;
}
int& get_bi()
{
return bi;
}
void display()
{
cout<<"bi = "<<bi<<endl;
}
};
class A
{
private:
B b;
int ai;
public:
A()
{
ai=b.get_bi();
}
void assign(int i)
{
ai=i;
This seems to be the only place where 'ai' is being changed
independently of 'b.bi'. So, add some code here to change 'b.bi'.
If you can't get to 'b.bi' directly (since it's private), use the
appropriate member function from 'B' that provides access to the
'bi' member of 'B'. If you can get a reference to the 'bi' member,
you should be able to assign the same 'i' value to it.

Please don't ask us to make code changes for you. This is too easy
not to be a homework, and we don't do homework for others.
}
void display()
{
cout<<"ai = "<<ai<<endl;
}
};
int main()
{
B bo;
bo.display();

A ao;
ao.display();
ao.assign(1111);
ao.display();
bo.display();

return 0;
}


V
--
Please remove capital As from my address when replying by mail
May 10 '06 #3
onkar wrote:
I want bi to be changed each time ai changes ,possibly using
references.Can anyone help me : bellow is the code - please suggest
changes -

#include<iostream>
using namespace std;
class B
{
private:
int bi;
public:
B()
{
bi=10;
}
int& get_bi()
{
return bi;
}
void display()
{
cout<<"bi = "<<bi<<endl;
}
};
class A
{
private:
B b;
int ai;
public:
A()
{
ai=b.get_bi();
}
void assign(int i)
{
ai=i;
}
void display()
{
cout<<"ai = "<<ai<<endl;
}
};
int main()
{
B bo;
bo.display();

A ao;
ao.display();
ao.assign(1111);
ao.display();
bo.display();

return 0;
}


The problem is, which bi and ai you want to bind? What if you have
multiple objects:

B bos[3];
A aos[3];

Which pair do you want to bind? bos[0].bi and aos[1].ai or bos[2] and
aos[0] ?

So if you really want to do this, you have to be explicit. Make a
function that clearly binds two objects together by value. For example:

void bind_B_to_A(B& b, A& a);
void bind_A_to_B(A& a, B& b);

B b; A a;
bind_B_to_A(b, a); // changing a.ai will change b.bi

There are a number of ways to do this. A simple way is to use a smart
pointer (such as a reference count pointer) to an int instead of a plain
int.

Regards,
Ben
May 10 '06 #4
actually this is a simpler version of the task i want to accomplish. I
want bi to change in class A. Actually I am using threads for that. so
How to go about doing it ?

May 10 '06 #5
onkar wrote:
actually this is a simpler version of the task i want to accomplish. I
want bi to change in class A. Actually I am using threads for that. so
How to go about doing it ?


You need to make A's member of class B to be a reference and not the object,
most likely. Then when you construct an A object, you can pass a B object
(by reference) to it and initialise the reference with it. Then when you
update the A's member, you would update the B's member through the reference
you have in the A.

V
--
Please remove capital As from my address when replying by mail
May 11 '06 #6

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

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
22
by: xmp333 | last post by:
Hi All, I am trying to hide my JavaScript source. The method I chose was to keep all the important source in a password protected folder, and then use a SRC="folder/script.js" to include it...
33
by: JKop | last post by:
I understand variables/objects and pointer variables perfectly: int X = 5; int* pX = &X; *pX = 4; int** ppX = &pX:
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
30
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines...
3
by: DonJefe | last post by:
Does anyone have experience using project->project references in large solutions? What are the plus/minuses that you have found? Currently, we are using the binary assembly references for our...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
3
by: CenturionX | last post by:
Hello everybody: I'd like to know what references in my vba code are used or not. I work in a code made by another person previously, i founded to many references and i believe that someones...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.