Why the following code compiles

#include<iostream> using namespace std; class Foo { void Bar( void ) const ; }; int main() { Foo f; cout<<sizeof(f)<<endl; } 

I ran this in g ++, it did not give me any compilation error. In addition, he completed the task o / p 1, which is correct. But I was expecting a binding error. Is this compiler dependent?

+4
source share
3 answers

I can only imagine that you expected to get an error because Foo::Bar not defined. The rule of one definition in the standard only requires that the elements used are used. In your particular case, nothing in your program uses Foo::Bar , so the program does not need this definition.

+11
source

This will be related because there are no great references to Foo :: Bar, and there it is not required to define it. If you were actually trying to make a call like f.bar (), this would give you an error.

+3
source

The linker error is missing because all dependencies are resolved.

As soon as you call the Bar() method and do not define it, you will get a linker error. Because then you are referencing Bar() , and the linker cannot solve it.

+3
source

All Articles