Passing a const reference as a function parameter in a class member function
Clash Royale CLAN TAG#URR8PPP
Passing a const reference as a function parameter in a class member function
Suppose we have a class called Line
that contains the following modifier that assigns a value to a class data member.
Line
void Start(const Point &start);
void Start(const Point &start);
The line class contains two data members called start and end which are both Point objects. Since the start
variable in the function parameter is a reference, and I implement it as follows:
start
void Line::Start(const Point &start)
m_start = start;
void Line::Start(const Point &start)
m_start = start;
In the main function I have the following.
Line line;
Point start(1, 1);
line.Start(start);
Line line;
Point start(1, 1);
line.Start(start);
Would the m_start
data member now refer directly to the start
Point
object in the main function since it was passed by reference?
m_start
start
Point
m_start
m_start
Point
Have you made any attempt to test this yourself, such as changing the point after and having another class method which outputs data from the passed
Point
reference to see if it is the same Point
?– Nick A
Aug 12 at 1:45
Point
Point
1 Answer
1
Would the m_start data member now refer directly to the start Point
object in the main function since it was passed by reference?
No. One of the properties of C++ references (and one of the ways in which their behavior differs from that of pointers) is that it's impossible to reseat a reference -- that is, you can't make a reference that was 'referring to' one thing later 'refer to' a different thing. (And of course if your m_start
member variable was of type Point
rather than type Point &
, then there's no reference to attempt-to-reseat anyway)
m_start
Point
Point &
Instead, start
's copy-constructor will be invoked to copy start
's contents into your m_start
member-variable.
start
start
m_start
The upshot is that your Start()
method will behave exactly like a similarly-implemented void Line::Start(Point start)
method would, with the following exceptions:
Start()
void Line::Start(Point start)
In your const Point &
version of Start()
, no separate/method-local copy of the Point object is made
const Point &
Start()
In your const Point &
version of Start()
, trying to modify start
from within the method would be a compile-time error (whereas in the pass-by-value version, the code inside Start()
could modify the argument, but the modifications would never be visible outside of the Start()
function since they'd only modify the local variable/parameter)
const Point &
Start()
start
Start()
Start()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Where is the declaration of
m_start
? Ifm_start
is aPoint
then no, the value is copied. If it is a const reference then you have a compile error. You need to initialize const members and references in the initializer list.– Retired Ninja
Aug 12 at 1:43