 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Darío Alejandro Guzik Guest
|
Posted: Tue Dec 05, 2006 7:39 pm Post subject: question about reference variables |
|
|
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
|
Posted: Tue Dec 05, 2006 8:08 pm Post subject: Re: question about reference variables |
|
|
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
|
Posted: Tue Dec 05, 2006 8:20 pm Post subject: Re: question about reference variables |
|
|
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
|
Posted: Tue Dec 05, 2006 8:24 pm Post subject: Re: question about reference variables |
|
|
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
|
Posted: Tue Dec 05, 2006 8:37 pm Post subject: Re: question about reference variables |
|
|
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
|
Posted: Tue Dec 05, 2006 10:40 pm Post subject: Re: question about reference variables |
|
|
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
|
Posted: Tue Dec 05, 2006 10:51 pm Post subject: Re: question about reference variables |
|
|
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 |
|
 |
|
|
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
|
|