What will be printed as the result of the program?
#include <iostream>
#include <string>

struct A {
  const std::string &a;
  A(const std::string &b) : a(b) {}
};

int main() {
  A a("test");
  std::cout << a.a;
}
Explanation
The result is undefined because the A::a reference will point to a temporary variable created in line 2 and deleted there. Therefore, the result of the program is not defined after this line. An execution error may or may not occur in line 3, since undefined behaviour

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes