reference attribut

T

Tony Johansson

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
 
S

Srini

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
 
P

Pete Becker

Tony said:
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.
 
A

Adrian

Tony Johansson said:
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.
 
A

Adrian

Adrian said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top