cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
user name:
password:
Forgot your password?
please wait
try again
cancel
forgot your password?
sign up
log in
[Legacy version]
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
passing reference into constructor
passing reference into constructor
Mar 26, 2009 at 5:50pm
Mar 26, 2009 at 5:50pm UTC
elabelle
(9)
If I have a class named "whatever" with a string member str, and it has the following constructor:
whatever(std::string & s) : str(s) {}
Does s get copied into str or do both s and str end up refering to the same string?
Last edited on
Mar 26, 2009 at 5:53pm
Mar 26, 2009 at 5:53pm UTC
Mar 26, 2009 at 6:18pm
Mar 26, 2009 at 6:18pm UTC
jsmith
(5804)
It depends on how you declared str. Is it a reference or not?
If it is a reference, then by definition they refer to the same string.
If it is not, then there are two separate string instances that can be
changed without affecting the other.
Mar 26, 2009 at 6:25pm
Mar 26, 2009 at 6:25pm UTC
elabelle
(9)
Ok, thanks.
Topic archived. No new replies allowed.