473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference attribut

Hello experts!

I have read in a book that say once a reference attribute has been assigned
a value, it cannot be changed but this is wrong I think because I can change
a reference attribute see the program below.

Now to my question have you any comment about this sentence once a reference
attribute has been assigned a value, it cannot be changed. Is it just
completely wrong or?
#include <string>
#include <iostream>
using namespace std;

class Motorfordon
{

public:
Motorfordon(string& name) : cname(name) {}

string getName()
{ return cname; }

void setName(string& name)
{ cname = name; }

private:
string& cname;

};

main()
{
string name("nisse");
Motorfordon f(name);
string nn("rulle");
f.setName(nn);
cout << f.getName() << endl;
}

Many thanks

//Tony
Aug 12 '05 #1
4 1598
> Hello experts!

I have read in a book that say once a reference attribute has been assigned
a value, it cannot be changed but this is wrong I think because I can change
a reference attribute see the program below.

Now to my question have you any comment about this sentence once a reference
attribute has been assigned a value, it cannot be changed. Is it just
completely wrong or?
#include <string>
#include <iostream>
using namespace std;

class Motorfordon
{

public:
Motorfordon(string& name) : cname(name) {}

string getName()
{ return cname; }

void setName(string& name)
{ cname = name; }

private:
string& cname;

};

main()
{
string name("nisse");
Motorfordon f(name);
string nn("rulle");
f.setName(nn);
cout << f.getName() << endl;

}

Many thanks

//Tony


References cannot be reseated - that's all there is to it. A referencr
*must* be initialized when it is declared. Once its initialized, its
just another alias to whatever it was initialized to. What you think as
changing the reference is actually changing the original thing that the
reference was initialized with. An example:

int a = 10;
int &ref = a; // ref is a reference to a
int b = 20;
ref = b; // what this is doing is changing the value of 'a'
to 20.
// ref is still a reference to 'a' itself!
std::cout << a; // this verifies that the value of 'a' is changed to
20.

In your example, you can try printing the value of 'name' in main. It
must have got changed to "rulle".

HTH
Srini

Aug 12 '05 #2
Tony Johansson wrote:

I have read in a book that say once a reference attribute has been assigned
a value, it cannot be changed but this is wrong I think because I can change
a reference attribute see the program below.


The statement is essentially correct. However, a reference has to be
initialized when it's created. After that you can't change it to refer
to some other object. When you assign a value to a reference as your
code does you're assigning to the object that it refers to:

int i = 0;
int j = 2;
int& ir = i;

ir = 3;
At this point, i has the value 3.

ir = j;
At this point, i has the value 2.

ir = 1;
At this point, i has the value 1. j has not been changed.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 12 '05 #3

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:Py*******************@newsb.telia.net...
Hello experts!

I have read in a book that say once a reference attribute has been assigned a value, it cannot be changed but this is wrong I think because I can change a reference attribute see the program below.
This is true as far as the language goes but in fact there is a way to reset
the reference in a member variable using this dirty trick (don't yell, it's
just to prove a point, I would not recomend it and I can't imagine why
anyone would use it).

template<typename T>
class ref_holder
{
const T& _ref;
public:
ref_holder(const ref_holder<T> & other) :
_ref(other.get())
{}
ref_holder(const T & ref) :
_ref(ref)
{}

const T& get() const {return _ref;}
void set(const T & ref)
{
new (this) ref_holder<T>(ref);
}
};

Where ref_holder can be used as bellow:

class MyClass
{
ref_holder<std::string> _stringRef;
public:
MyClass(const std::string & val) :
_stringRef(val)
{}

const std::string& get_val() const {return _stringRef.get();}
void set_val(const std::string& val) {_stringRef.set(val);}
};

Now to my question have you any comment about this sentence once a reference attribute has been assigned a value, it cannot be changed. Is it just
completely wrong or?
#include <string>
#include <iostream>
using namespace std;

class Motorfordon
{

public:
Motorfordon(string& name) : cname(name) {}

string getName()
{ return cname; }

void setName(string& name)
{ cname = name; }
Here you assign name to the object refrenced by cname to the the cname
reference. A reference is to be treated as another name for an object
instance.

private:
string& cname;

};

main()
{
string name("nisse");
Motorfordon f(name);
string nn("rulle");
f.setName(nn);
cout << f.getName() << endl;
}

Many thanks

//Tony

Aug 12 '05 #4

"Adrian" <ba******@xoasis.com> wrote in message
news:42********@news.microsoft.com...
Here you assign name to the object refrenced by cname to the the cname
reference. A reference is to be treated as another name for an object
instance.

Typo, I meant:
Here you assign name to the object refrenced by cname NOT to the the cname
reference. A reference is to be treated as another name for an object
instance.
Aug 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13105
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
5
1695
by: khaled Hajjar | last post by:
Hello, I have this template : <root> <categorie> <poste att = 'true'> ..... </poste> <poste att = 'false'> .....
110
9812
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
2972
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
11
2173
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
17903
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
3
1032
by: Marc Robitaille | last post by:
Hello, I have this Field attribut class and a Client class that uses this Attribut Class <AttributeUsage(AttributeTargets.Property)_ Public Class Field Inherits Attribute Private _FieldName...
41
3607
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
275
12035
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.