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(str ing& 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 4 1605
> 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(str ing& 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
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)
"Tony Johansson" <jo************ *****@telia.com > wrote in message
news:Py******** ***********@new sb.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<typena me T>
class ref_holder
{
const T& _ref;
public:
ref_holder(cons t ref_holder<T> & other) :
_ref(other.get( ))
{}
ref_holder(cons t T & ref) :
_ref(ref)
{}
const T& get() const {return _ref;}
void set(const T & ref)
{
new (this) ref_holder<T>(r ef);
}
};
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(str ing& 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
"Adrian" <ba******@xoasi s.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Unixware/C++/AT&T componets
environment.
I have been able to compile all modules after making necessary changes
in LINUX/gcc/STL environment. We have two templates defined XList and
XMap.
|
by: khaled Hajjar |
last post by:
Hello,
I have this template :
<root>
<categorie>
<poste att = 'true'>
.....
</poste>
<poste att = 'false'>
.....
|
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 must be an object
instead of
|
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 tell me which one will be more efficient and why?
as far as i know both must be same, because i read somewhere that internally
|
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?
| |
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 create another
Area and assign myArea to it (ie: Area foo = myArea;) or is there a
better way?
~AF
|
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 As String
Private _Changed As Boolean
|
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 pointer and reference are and how they
relate
to each other.
|
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'
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |