Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does the following code compile

Tags:

c++

linker

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

I ran this on g++,it did not give me any compilation error. Also, it executed giving o/p 1 which is correct. But I was expecting, error during linking. Is this compiler dependent?

like image 990
rahul Avatar asked Dec 02 '22 23:12

rahul


1 Answers

I can only imagine that you expected to get an error as Foo::Bar is not defined. The One Definition Rule in the standard requires only that used elements are defined. In your particular case, nothing in your program uses Foo::Bar, so the program does not need that definition.

like image 102
David Rodríguez - dribeas Avatar answered Dec 19 '22 21:12

David Rodríguez - dribeas