BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

question about reference variables

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Darío Alejandro Guzik
Guest





PostPosted: Tue Dec 05, 2006 7:39 pm    Post subject: question about reference variables Reply with quote



Hi all!

I have a program I'm working on that has a lot of classes with members
that are references to let's say global instances of other classes.

my problem is that We detected a problem and I don't know an easy way to
solve it.

Example:

class T1 {
...
}

class T2 {
T1& val;
...
operator= (const T2 & src) { val = src.val;....};
}

the problem is that when I assign val from another instance of T2 I want
it to reference the original value, and not the local variable of the
other T2 instance.

Some simple practical example:

int main(int argc, char* argv[])
{
int a = 8;
int &b = a; //b references a value
int &c = b; //c references b value, but I want it to reference
directly a
b = 5; //this is my problem, when b changes, c changes as well
because c is referencing b and not a
return 0;
}


Is there a way to implement this without using pointers?

Thanks
Back to top
Vladimir Grigoriev
Guest





PostPosted: Tue Dec 05, 2006 8:08 pm    Post subject: Re: question about reference variables Reply with quote



In your example int a, b, c reference the same memory address. So if you
will change b, a and c will be changed also.

Vladimir Grigoriev

"Darío Alejandro Guzik" <dguzik (AT) papyro (DOT) com> wrote in message
news:4575766f (AT) newsgroups (DOT) borland.com...
Quote:
Hi all!

I have a program I'm working on that has a lot of classes with members
that are references to let's say global instances of other classes.

my problem is that We detected a problem and I don't know an easy way to
solve it.

Example:

class T1 {
...
}

class T2 {
T1& val;
...
operator= (const T2 & src) { val = src.val;....};
}

the problem is that when I assign val from another instance of T2 I want
it to reference the original value, and not the local variable of the
other T2 instance.

Some simple practical example:

int main(int argc, char* argv[])
{
int a = 8;
int &b = a; //b references a value
int &c = b; //c references b value, but I want it to reference
directly a
b = 5; //this is my problem, when b changes, c changes as well
because c is referencing b and not a
return 0;
}


Is there a way to implement this without using pointers?

Thanks
Back to top
Darío Alejandro Guzik
Guest





PostPosted: Tue Dec 05, 2006 8:20 pm    Post subject: Re: question about reference variables Reply with quote



Ok... I missed that... it makes sense but... is there a way to
re-initialize a reference or once assigned it is fixed, I mean, b will
always reference the memory adress of a or I can change it somehow?

Now refreshing my brains I kind of remember that reference variables in
c must be initialized on creation and cannot be modified (the L
value)... is it right?

Vladimir Grigoriev wrote:
Quote:
In your example int a, b, c reference the same memory address. So if you
will change b, a and c will be changed also.

Vladimir Grigoriev

"Darío Alejandro Guzik" <dguzik (AT) papyro (DOT) com> wrote in message
news:4575766f (AT) newsgroups (DOT) borland.com...
Hi all!

I have a program I'm working on that has a lot of classes with members
that are references to let's say global instances of other classes.

my problem is that We detected a problem and I don't know an easy way to
solve it.

Example:

class T1 {
...
}

class T2 {
T1& val;
...
operator= (const T2 & src) { val = src.val;....};
}

the problem is that when I assign val from another instance of T2 I want
it to reference the original value, and not the local variable of the
other T2 instance.

Some simple practical example:

int main(int argc, char* argv[])
{
int a = 8;
int &b = a; //b references a value
int &c = b; //c references b value, but I want it to reference
directly a
b = 5; //this is my problem, when b changes, c changes as well
because c is referencing b and not a
return 0;
}


Is there a way to implement this without using pointers?

Thanks

Back to top
Vladimir Grigoriev
Guest





PostPosted: Tue Dec 05, 2006 8:24 pm    Post subject: Re: question about reference variables Reply with quote

You cannot reassign a reference. Once a reference is declared it reference
the same memory during its life.

Vladimir Grigoriev

"Darío Alejandro Guzik" <dguzik (AT) papyro (DOT) com> wrote in message
news:45757ffe (AT) newsgroups (DOT) borland.com...
Quote:
Ok... I missed that... it makes sense but... is there a way to
re-initialize a reference or once assigned it is fixed, I mean, b will
always reference the memory adress of a or I can change it somehow?

Now refreshing my brains I kind of remember that reference variables in
c must be initialized on creation and cannot be modified (the L
value)... is it right?

Vladimir Grigoriev wrote:
In your example int a, b, c reference the same memory address. So if you
will change b, a and c will be changed also.

Vladimir Grigoriev

"Darío Alejandro Guzik" <dguzik (AT) papyro (DOT) com> wrote in message
news:4575766f (AT) newsgroups (DOT) borland.com...
Hi all!

I have a program I'm working on that has a lot of classes with members
that are references to let's say global instances of other classes.

my problem is that We detected a problem and I don't know an easy way to
solve it.

Example:

class T1 {
...
}

class T2 {
T1& val;
...
operator= (const T2 & src) { val = src.val;....};
}

the problem is that when I assign val from another instance of T2 I want
it to reference the original value, and not the local variable of the
other T2 instance.

Some simple practical example:

int main(int argc, char* argv[])
{
int a = 8;
int &b = a; //b references a value
int &c = b; //c references b value, but I want it to reference
directly a
b = 5; //this is my problem, when b changes, c changes as well
because c is referencing b and not a
return 0;
}


Is there a way to implement this without using pointers?

Thanks

Back to top
Darío Alejandro Guzik
Guest





PostPosted: Tue Dec 05, 2006 8:37 pm    Post subject: Re: question about reference variables Reply with quote

thanks... I was realizing that while asking... sometimes it helps :P

Thanks again

Vladimir Grigoriev wrote:
Quote:
You cannot reassign a reference. Once a reference is declared it reference
the same memory during its life.

Vladimir Grigoriev

"Darío Alejandro Guzik" <dguzik (AT) papyro (DOT) com> wrote in message
news:45757ffe (AT) newsgroups (DOT) borland.com...
Ok... I missed that... it makes sense but... is there a way to
re-initialize a reference or once assigned it is fixed, I mean, b will
always reference the memory adress of a or I can change it somehow?

Now refreshing my brains I kind of remember that reference variables in
c must be initialized on creation and cannot be modified (the L
value)... is it right?

Vladimir Grigoriev wrote:
In your example int a, b, c reference the same memory address. So if you
will change b, a and c will be changed also.

Vladimir Grigoriev

"Darío Alejandro Guzik" <dguzik (AT) papyro (DOT) com> wrote in message
news:4575766f (AT) newsgroups (DOT) borland.com...
Hi all!

I have a program I'm working on that has a lot of classes with members
that are references to let's say global instances of other classes.

my problem is that We detected a problem and I don't know an easy way to
solve it.

Example:

class T1 {
...
}

class T2 {
T1& val;
...
operator= (const T2 & src) { val = src.val;....};
}

the problem is that when I assign val from another instance of T2 I want
it to reference the original value, and not the local variable of the
other T2 instance.

Some simple practical example:

int main(int argc, char* argv[])
{
int a = 8;
int &b = a; //b references a value
int &c = b; //c references b value, but I want it to reference
directly a
b = 5; //this is my problem, when b changes, c changes as well
because c is referencing b and not a
return 0;
}


Is there a way to implement this without using pointers?

Thanks


Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Tue Dec 05, 2006 10:40 pm    Post subject: Re: question about reference variables Reply with quote

Please direct your browser at http://info.borland.com/newsgroups/ and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!
Back to top
Darío Alejandro Guzik
Guest





PostPosted: Tue Dec 05, 2006 10:51 pm    Post subject: Re: question about reference variables Reply with quote

sorry

Thomas Maeder [TeamB] wrote:
Quote:
Please direct your browser at http://info.borland.com/newsgroups/ and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) 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.