LWN.net Logo

Quote of the week

Quote of the week

Posted Dec 11, 2004 12:51 UTC (Sat) by mmutz (guest, #5642)
In reply to: Quote of the week by kleptog
Parent article: Quote of the week

The compiler needs to know at compile time all possible variations of that class.

If this is true, please explain to me how on earth the compiler manages to compile this code:

 
foo.h: 
class Foo { 
public: 
  virtual void f(); 
}; 
 
foo.cpp: 
#include "foo.h" 
void Foo::f() {} 
 
bar.h: 
#include "foo.h" 
class Bar : public Foo { 
public: 
  void f(); 
}; 
 
bar.cpp: 
#include "bar.h" 
void Bar::f() {} 
using something like
cc -o foo.o -c foo.cpp 
cc -o bar.o -c bar.cpp 
cc -o foobar foo.o bar.o 


(Log in to post comments)

Quote of the week

Posted Dec 11, 2004 17:23 UTC (Sat) by simlo (subscriber, #10866) [Link]

Make the examble better by making f pure virtual in the base class: Then you eliminate the problem of uninitialized function pointers compile time! C++ will simply not allow you to initialize an object of this class before a method have been specified.

By having structures of function pointers there is no check on the actual pointers point to anything sensible. The only thing safe to do is to check if they are non-NULL before using them! Waste of instructions...

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds