469,572 Members | 1,214 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

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 1435
> 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Sandy | last post: by
13 posts views Thread by Abe Frohnman | last post: by
3 posts views Thread by Marc Robitaille | last post: by
41 posts views Thread by Summercool | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.