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
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