C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ References

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
onkar
Guest





PostPosted: Wed May 10, 2006 9:21 pm    Post subject: C++ References Reply with 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;
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





PostPosted: Thu May 11, 2006 6:21 pm    Post subject: Re: C++ References Reply with quote



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





PostPosted: Thu May 11, 2006 6:21 pm    Post subject: Re: C++ References Reply with quote



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





PostPosted: Thu May 11, 2006 11:25 pm    Post subject: Re: C++ References Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.