Lecture 14 Flashcards

1
Q

const Object* obj;

A

The referenced object obj is considered constant and can’t be modified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
void someMethod(const Object* obj)
Proper way to call this?
A

const Object obj();
someMethod(&obj);

Object* obj = new Object();
someMethod(obj);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
call someMethod
void someMethod(const string &str)
A

someMethod(string(“hellow world”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can const be applied to return values?

A

Yes, this will return it while blocking write access within the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly