0

I would like to understand how do classes compile in C++.

Why does the code below compile successfully? Doesn't Foo() need an implementation for compilation to be successful?

class Test{
public:
    Test()  {}
    int Foo();
};

int main()
{
    Test obj;
    return 0;
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Emadpres
  • 3,466
  • 2
  • 29
  • 44

2 Answers2

7

Nobody tries to call Foo, so the linker doesn't complain about the missing implementation, because it's not needed.

If you wrote virtual int Foo();, you would see a linker error.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
2

There is no Book() in your code. If you are talking about Foo(), you are never using that function within your code, so the linker never gets asked about it and fail with "undefined function" or some similar error.

K-ballo
  • 80,396
  • 20
  • 159
  • 169