0% found this document useful (0 votes)
1 views7 pages

W2 L3 Activity References Solutions1

The document discusses the use of const references and pointers in C++, highlighting common errors when attempting to modify referenced objects or pointers. It explains the difference between passing by value and passing by reference, demonstrating how object attributes can be modified through references. Additionally, it covers the implications of assigning to references and pointers, emphasizing that assignments affect the objects they reference rather than changing the reference itself.

Uploaded by

zarif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

W2 L3 Activity References Solutions1

The document discusses the use of const references and pointers in C++, highlighting common errors when attempting to modify referenced objects or pointers. It explains the difference between passing by value and passing by reference, demonstrating how object attributes can be modified through references. Additionally, it covers the implications of assigning to references and pointers, emphasizing that assignments affect the objects they reference rather than changing the reference itself.

Uploaded by

zarif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Activity: References

Solutions

05/29/2025 Copyright 2024 Samuel Midkiff, Steve France, Sanjay Rao, and J 1
enna DiVincenzo
Q1: Const references/parameters:
Examples of erroneous actions (See Example11 code)

• In what follows, the reference or pointer has


void bar (Object& ref, Object* ptr) {
been passed in through a const parameter
ref.attribute = 0;
ptr->attribute = 0; 1. If you try and change the referenced
} object, or change what a pointer points
to, you get an error
void foo(const Object& ref, const Object* ptr) { 2. If you try and pass the reference or
// ref.attribute = 0; pointer to a non-const parameter of
// ptr->attribute = 0; another function, you get an error
// bar(ref, ptr); 3. Converting from a const pointer or const
// Object* ptr2 = ptr; reference to a non-const pointer is not
// Object* ptr4 = &ref; allowed, you get an error
} • In general, anything you do that leads to the
referenced object or pointed-to object being
changed will give you an error
• Be wary: doing some ugly stuff can
bypass constness if you really try.
05/29/2025 Copyright 2024 Samuel Midkiff, Steve France, Sanjay Rao, and J 2
enna DiVincenzo
Q2: Passing references to objects and vice versa
(See Example12 code) int main(int argc, char* argv[ ]) {
class Obj { void foo1(Obj objVar) {
std::cout << "foo1" << std::endl; Obj oVar(-1);
public:
objVar.i ++; Obj& oRef = oVar;
int i; foo1(oVar); // S0
Obj(int); }
std::cout << oVar.i << std::endl; // -1
}; foo1(oRef); // S1
void foo2(Obj& objRef) {
Obj::Obj(int _i) { std::cout << "foo2" << std::endl; std::cout << oVar.i << std::endl; // -1
i = _i; objRef.i ++; foo2(oVar); // S2
} }
std::cout << oVar.i << std::endl; // 0
Answer: foo2(oRef); // S3
• S0 and S1 will not lead to oVar.i in main being std::cout << oVar.i << std::endl; // 1
incremented
}
• foo1: Pass by value. Even if we pass a reference arg (S1), the object bound to the
reference is passed by value and copied into the function.
• Both S2 and S3 will lead to oVar.i being incremented.
• foo2: Pass by reference. When we pass an object to a reference parameter (S2) a
reference to the object is created and passed into the function.
05/29/2025 Copyright 2024 Samuel Midkiff, Steve France, Sanjay Rao, and J 3
enna DiVincenzo
Q3: Assigning to references
(See Example13 code) obj1R i: 1
class Obj { int main(int argc, char* argv[ ]) {
public: Obj obj1 = Obj(1);
int i; Obj& obj1R= obj1; objR2 i: 2
Obj(int); Obj obj2(2);
}; Obj& obj2R = obj2;
std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
Obj::Obj(int _i) { obj1R = obj2R;
i = _i; std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
} obj1.i = 500;
std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
Output: }
obj1R: 1, obj2R: 2
The references are bound to an object when they are declared and defined. They are
always bound to that object.
05/29/2025 Copyright 2024 Samuel Midkiff, Steve France, Sanjay Rao, and
4
Jenna DiVincenzo
Q3: Assigning to references obj1R i: 2
class Obj {
public:
int main(int argc, char* argv[ ]) { objR2 i: 2
int i;
...
Obj(int);
std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
};
obj1R = obj2R;
std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
Obj::Obj(int _i) {
obj1R.i = 500;
i = _i;
std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
} Output: }
obj1R: 1, obj2R: 2
obj1R: 2, obj2R: 2
Assigning to a reference is the same as assigning to the object they are bound to. Thus, the
assignment changes the value of the obj1 reference, but does not change what object obj1
references
05/29/2025 Copyright 2024 Samuel Midkiff, Steve France, Sanjay Rao, and
5
Jenna DiVincenzo
Q3: Assigning to references obj1R i: 500
class Obj {
public: int main(int argc, char* argv[ ]) {
int i; ... objR2 i: 2
Obj(int); obj1R = obj2R;
}; std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
obj1R.i = 500;
Obj::Obj(int _i) { std::cout << "obj1R: " << obj1R.i << ", obj2R: " << obj2R.i << std::endl;
i = _i; }
Output:
}
obj1R: 1, obj2R: 2
obj1R: 2, obj2R: 2
obj1R: 500, obj2R: 2
Thus, the assignment changes the value of the obj1 reference, but does not change
what object obj1 references
05/29/2025 Copyright 2024 Samuel Midkiff, Steve France, Sanjay Rao, and
6
Jenna DiVincenzo
Q4: Assigning to pointers
(Example14 code)
class Obj { int main(int argc, char* argv[ ]) { obj1 i: 1
public: Obj* obj1 = new Obj(1);
int i; Obj* obj2 = new Obj(2);
Obj(int); obj1 = obj2; obj2 i: 2
}; obj1->i=500;
obj1 = obj2
} executes
Obj::Obj(int _i) {
i = _i;
}
obj1 i: 1 obj1 i: 1
Output: obj1->i = 500
obj1: 1, obj2: 2 executes
obj1: 2, obj2: 2
obj2 i: 2 obj2 i: 500
obj1: 500, obj2: 500
05/29/2025 Copyright 2024 Samuel Midkiff, Steve France, Sanjay Rao, and J 7
enna DiVincenzo

You might also like