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

What is line 2 and 3? They are in a main() function?

2020 Aug 6, 9:16:45 AM

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты