From: R DOT G DOT Morgan AT ncl DOT ac DOT uk Newsgroups: comp.os.msdos.djgpp Subject: RE: Style AND speed in C++? How? Please, I need some radical ideas on this one! Date: 24 Apr 1997 12:14:55 GMT Organization: University of Newcastle upon Tyne Lines: 37 Sender: n6368757 AT glen13 DOT ncl DOT ac DOT uk (R.G. Morgan) Distribution: world Message-ID: <5jnirv$h2p@whitbeck.ncl.ac.uk> References: <861717941.0523452.0@[194.129.18.166]> NNTP-Posting-Host: glen13.ncl.ac.uk To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Hate to disagree with Shawn, but I think that in gcc, as in many other compilers (I've not checked my idea in the source), having virtual functions in a class creates a virtual table. This virtual table is then referenced by a virtual table pointer in the object (struct) generated. When a virtual function is called , the virtual pointer in the struct is derefenced to the virtual table, which contains pointers to the appropriate functions. This saves space on a per-instance basis (eg for a class with 2 virtual functions, 1 pointer extra is embedded in the class, as opposed to 2), but costs time. I'd agree with Doug Eleveld: use function pointers in your struct for absolute speed, if there will only be a few instances. Of course, this means setting them up yourself, rather than letting the compiler do all the work. In article <861717941.0523452.0@[194.129.18.166]>, Shawn Hargreaves writes: |> Doug Eleveld writes: |> > I'm pretty sure that the overhead of a virtual function call could cause |> > you some problems. It's OK to use virtual function for setup or |> > something but don't use it for putpixel or linedraw. The fastest and |> > neatest way that I can think of is to keep function pointers for all the |> > fast drawing functions, and fill them in in the constructor of whatever |> > video card. |> |> You seem to be missing the point here: virtual functions _are_ function |> pointers! That's all the virtual keyword means: it just tells the C++ |> compiler to allocate some space in the structure, fill it in with a pointer |> to the code, and use this pointer whenever you call the function rather |> than branching directly to it. That's exactly the same thing as using |> function pointers directly in C, as I did in Allegro, but easier because |> most of the hassle is taken care of by the compiler... |> |> Shawn Hargreaves. |> |> R DOT G DOT Morgan AT ncl DOT ac DOT uk