Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ References

Reply
Thread Tools

C++ References

 
 
onkar
Guest
Posts: n/a
 
      05-10-2006
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;
}

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-10-2006
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;
> }


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-10-2006
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


 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      05-10-2006
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
 
Reply With Quote
 
onkar
Guest
Posts: n/a
 
      05-10-2006
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 ?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-11-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Snake references just as ok as Monty Python jokes/references in python community? :) seberino@spawar.navy.mil Python 8 12-12-2006 11:21 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57