473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Differences between these three signatures....

xz
I just wanna confirm my understanding about the differences between
the signatures as follows, which are called all in the same way:

void foo(Vertex a, Vertex b, Vertex c);
void foo(Vertex & a, Vertex & b, Vertex & c);
void foo(const Vertex & a, const Vertex & b, const Vertex & c);

In the first one, the function actually works on the local copies of
a, b and c. Whatever happens inside foo does not change anything in
the outside world.

In the second one, the function operates on the original copies of a,
b and c. Whatever foo does to a, b and c inside the function
immediately applies on the a, b and c in the outside world.

In the third one, the function operates on the original copies of a, b
and c. However it cannot change anything about a, b or c.

Is that always true, no matter how the copy-constructor of Vertex is
defined?

Basically, people use the first way when they need to only locally
change something of a, b, c but do not want that change influence the
original a, b, and c in the outside world.

People use the second way when they do want the changes to a, b, and c
apply to the original a, b, and c in the outside world. In this case,
a, b, c are actually like returned result.

People use the third way when they don't need to change anything about
a, b, and c in the function, i.e. they only read a, b, and c, and
meanwhile, they want to promote the memory-efficiency.

Is that understanding correct?
Mar 15 '08 #1
2 1270
On Mar 15, 10:18*pm, xz <zhang.xi...@gmail.comwrote:
I just wanna confirm my understanding about the differences between
the signatures as follows, which are called all in the same way:

void foo(Vertex a, Vertex b, Vertex c);
void foo(Vertex & a, Vertex & b, Vertex & c);
void foo(const Vertex & a, const Vertex & b, const Vertex & c);

In the first one, the function actually works on the local copies of
a, b and c. Whatever happens inside foo does not change anything in
the outside world.
Yes, provided that the copy constructor for Vertex is accessible.
In the second one, the function operates on the original copies of a,
b and c. Whatever foo does to a, b and c inside the function
immediately applies on the a, b and c in the outside world.
yes, if by "outside world" you mean the objects in the caller
function. Further, if the caller function has these objects as const /
temporaries, they cannot be used as arguments to foo - since foo takes
non-const references.
In the third one, the function operates on the original copies of a, b
and c. However it cannot change anything about a, b or c.
true.
Is that always true, no matter how the copy-constructor of Vertex is
defined?
Provided it is accessible. (2) and (3) use references, and hence donot
need a copy constructor.
Basically, people use the first way when they need to only locally
change something of a, b, c but do not want that change influence the
original a, b, and c in the outside world.

People use the second way when they do want the changes to a, b, and c
apply to the original a, b, and c in the outside world. In this case,
a, b, c are actually like returned result.

People use the third way when they don't need to change anything about
a, b, and c in the function, i.e. they only read a, b, and c, and
meanwhile, they want to promote the memory-efficiency.

Is that understanding correct?
AFAIK, yes.

Mar 15 '08 #2
On 15 mar, 18:18, xz <zhang.xi...@gmail.comwrote:
I just wanna confirm my understanding about the differences
between the signatures as follows, which are called all in the
same way:
void foo(Vertex a, Vertex b, Vertex c);
void foo(Vertex & a, Vertex & b, Vertex & c);
void foo(const Vertex & a, const Vertex & b, const Vertex & c);
In the first one, the function actually works on the local copies of
a, b and c. Whatever happens inside foo does not change anything in
the outside world.
In the second one, the function operates on the original copies of a,
b and c. Whatever foo does to a, b and c inside the function
immediately applies on the a, b and c in the outside world.
In the third one, the function operates on the original copies of a, b
and c. However it cannot change anything about a, b or c.
Sort of. That's generally the intent, but from a language point
of view: it can modify mutable elements, it can cast away the
const (which is generally considered very bad programming
style), and it can modify the object through other expressions
which refer to it---if e.g. the function is called with a
referring to a global variable, it can always access the global
variable.
Is that always true, no matter how the copy-constructor of Vertex is
defined?
Sort of. If the copy-constructor isn't accessible, you can't
call the first at all, and you can't call the third with a
temporary (although this restriction is being dropped, I think).
Basically, people use the first way when they need to only
locally change something of a, b, c but do not want that
change influence the original a, b, and c in the outside
world.
People use the second way when they do want the changes to a,
b, and c apply to the original a, b, and c in the outside
world. In this case, a, b, c are actually like returned
result.
People use the third way when they don't need to change
anything about a, b, and c in the function, i.e. they only
read a, b, and c, and meanwhile, they want to promote the
memory-efficiency.
Is that understanding correct?
Basically. They also use one of the last two when the object
doesn't support copy, which is a fairly frequent case in many
applications.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 16 '08 #3

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

Similar topics

6
by: Martin Meyer im Hagen | last post by:
Hello, I've got installed Win 2003 SBS Premium with the SQL Server 2000 on a server machine. It works almost fine, except the application which uses the SQL Server. The main part of the...
31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
2
by: Patrick | last post by:
Are the differences between a search engine, a subject directory and a meta search engine significant for an ebusiness web site owner? A meta search engine merely uses ordinary existing search...
13
by: Kieran | last post by:
I am designing a content management system and I want to make sure all pages entered into it's database by users are using valid HTML. I have designed the system to use HTML 4.01 Transitional...
1
by: karflips33 | last post by:
Is it possible to automate the signing of Word 2003 docs with Digital Signatures? My problem with the code approach using ActiveDocument.Signatures.Add
74
by: Dominik Wallner | last post by:
Hi! I'm currently implementing a program which measures voltages through an external USB-AD-converter. It should output those values as time/voltage pairs. My problem is to measure the time...
4
by: jianyi | last post by:
Hi, I am working on an xml project, and I am trying to understand something. if the author needs to sign one xml file, then his manager will counter-sign, and a third witness will sign, can...
2
by: John Brawley | last post by:
Please what is the functional C++ difference between a string of characters typed manually and included inside the main() function of a program... vis: string str = "123,4.56,7.8"; , ....and...
3
by: gentsquash | last post by:
Inside of <script></script>, inside of <body></body>, what are the differences between document.body.className="mystyle"; and document.body.setAttribute("class", "mystyle"); ? (...assuming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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
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
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
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...
0
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...

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.