C++ Quiz36 of 173
Difficulty:
⬅️ Prev: 49Quiz ID: 52Next: 105 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

class A;

class B {
public:
  B() { std::cout << "B"; }
  friend B A::createB();
};

class A {
public:
  A() { std::cout << "A"; }

  B createB() { return B(); }
};

int main() {
  A a;
  B b = a.createB();
}
Hint

What does B have to know about A in order to declare A::createB() a friend?

Reveal answer

The program has a compilation error

Explanation

There is a compilation error when attempting to declare A::createB() a friend of B. To declare A::createB() a friend of B, the compiler needs to know that that function exists. Since it has only seen the declaration of A so far, not the full definition, it cannot know this.

⬅️ Prev: 49Quiz ID: 52Next: 105 ➡️