 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
onkar Guest
|
Posted: Wed May 10, 2006 9:21 pm Post subject: C++ References |
|
|
I want bi to change as soon as i change ai. How to accomplish this
using references ?
#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;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Thu May 11, 2006 6:21 pm Post subject: Re: C++ References |
|
|
In article <1147258067.199029.103000 (AT) i39g2000cwa (DOT) googlegroups.com>,
onkar <onkar.n.m (AT) gmail (DOT) com> wrote:
| Quote: | I want bi to change as soon as i change ai. How to accomplish this
using references ?
#include<iostream
using namespace std;
class B
{
private:
int bi;
public:
B()
{
bi=10;
}
int & get_bi() {return bi;}
[snip]
};
class A
{
private:
B b;
int ai;
public:
A()
{
ai=b.get_bi();
[snip]
|
class A
{
B b;
int &ai;
public:
A() :b(),a(b.get_bi()){}
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ben Cottrell Guest
|
Posted: Thu May 11, 2006 6:21 pm Post subject: Re: C++ References |
|
|
onkar wrote:
| Quote: | I want bi to change as soon as i change ai. How to accomplish this
using references ?
|
Declare bi as a reference, and initialise it to ai in the constructor.
Similar to this example:
class base
{
int a;
protected:
int& getA() {return a;}
};
class derived : public base
{
int& b;
public:
derived() : b(getA()) {}
};
note that the changes go both ways, because effectively, they are both
alternative names for the same object.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Zara Guest
|
Posted: Thu May 11, 2006 11:25 pm Post subject: Re: C++ References |
|
|
On 10 May 2006 17:02:31 -0400, "onkar" <onkar.n.m (AT) gmail (DOT) com> wrote:
| Quote: | I want bi to change as soon as i change ai. How to accomplish this
using references ?
#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;
chage to: |
int& ai;
So that it really is a reference, not a value by itself
| Quote: | public:
A()
{
ai=b.get_bi();
}
change to: |
a():ai(b.get_bi()) {}
The reference must be initialized on the constructor list, or you
should get an error from the compiler. From now on, any change to ai
will change the value of the object referred by ai (in this case, bi).
| Quote: | void assign(int i)
{
ai=i;
}
void display()
{
cout<<"ai = "<<ai<<endl;
}
};
... |
Regards,
Zara
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| 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
|
|