Lecture 14 Flashcards
1
Q
const Object* obj;
A
The referenced object obj is considered constant and can’t be modified
2
Q
void someMethod(const Object* obj) Proper way to call this?
A
const Object obj();
someMethod(&obj);
Object* obj = new Object(); someMethod(obj);
3
Q
can const objects be passed by reference to non-const functions?
A
As there is no guarantee that the referenced object will not be modified when passed to a non-const parameter, the compiler blocks this.
4
Q
call someMethod void someMethod(const string &str)
A
someMethod(string(“hellow world”));
5
Q
Can const be applied to return values?
A
Yes, this will return it while blocking write access within the object.
6
Q
benefits of void someMethod(conststring &str)
A
Since str is passed by reference here, the system doesn’t have to copy its value
This makes the program more efficient in terms of run-time and in terms of memory use