You've answered 0 of 177 questions correctly. (Clear)
Question #114 Difficulty:
According to the C++23 standard, what is the output of this program?
#include <iostream>
#include <memory>
#include <vector>
class C {
public:
void foo() { std::cout << "A"; }
void foo() const { std::cout << "B"; }
};
struct S {
std::vector<C> v;
std::unique_ptr<C> u;
C *const p;
S()
: v(1)
, u(new C())
, p(u.get())
{}
};
int main() {
S s;
const S &r = s;
s.v[0].foo();
s.u->foo();
s.p->foo();
r.v[0].foo();
r.u->foo();
r.p->foo();
}
Hint:
How does constness of an object affect members?
Is the object that a const pointer points to also const?
What is the constness of an object returned by a const vector::operator[]
?
Problems? View a hint or try another question.
I give up, show me the answer (make 3 more attempts first).
Mode : Training
You are currently in training mode, answering random questions. Why not Start a new quiz? Then you can boast about your score, and invite your friends.
Contribute
Android app
Get Sergey Vasilchenko's CppQuiz Android app.
C++ Brain Teasers
Get the book, support the site!