Sign in to follow this  
Quak

Questions on C++

Recommended Posts

I've got a few questions on C++ 1. If you overload an operator does this work like a function and creates the variables on the stack ? 2. If so can you inline operator overloads ? 3. If not would it be faster to use a inline method e.g. for a vector class vector.addvector(vector v) instead of v1 + v2 ? 4. I have heard C++ is not portable to another sytem in some cases were C can be ported. What is the reason for this ? 5. Should I try to avoid polymorphy and function pointers to avoid a virtual table to make my code faster ? 6. Once I use a virtual table does it matter to code speed how many virtual classes I use? Thanks :) Quak

Share this post


Link to post
Share on other sites
ok, here are my answers:

Quote:
1. If you overload an operator does this work like a function
and creates the variables on the stack ?

It is a normal function that will be called at the place in your source where you use your overloaded operator.

Quote:
2. If so can you inline operator overloads ?

Yes that is allowed.

Quote:

3. If not would it be faster to use a inline method e.g. for a
vector class vector.addvector(vector v) instead of v1 + v2 ?

No that would not be faster, but sometimes better to read.

Quote:

5. Should I try to avoid polymorphy and function pointers to
avoid a virtual table to make my code faster ?

Yes, a virtual table look up makes your code slightly slower.

Quote:
6. Once I use a virtual table does it matter to code speed how
many virtual classes I use?

I guess so. If you have many items in a list it takes longer to finde something in it (but I have never heard that this was the cause for problems)

-- constantin

Share this post


Link to post
Share on other sites
6. The virtual function table is essentially an array of function pointers, and each virtual function is given an index. Calling a virtual function is the same as indexing an array (although they implement it slightly differently).

Share this post


Link to post
Share on other sites
5) Avoid? No [smile] Unless you really need an extra speed. Otherwise polymorphism along with vtable is fast enough for almost all puposes. And if profiling shows up it's not, then remove it. But not unless profiling has shown up it's the real bottleneck.

EDIT [0]: Fixed the word "polymorphism". Thanks Oluseyi [smile]

Oxyd

[Edited by - Oxyd on October 18, 2004 12:53:22 PM]

Share this post


Link to post
Share on other sites
Use virtual functions where appropriate. They don't really add overhead. Like I mentionned in another thread, in the Quake 2 source code, they actually imitate the behavior of virtual functions in C by using function pointers to specify which function is going to be called to update the behavior of a monster... The virtual functions are just an automatic way to solve a problem that presents itself naturally.

Share this post


Link to post
Share on other sites
Quote:
4. I have heard C++ is not portable to another sytem in some
cases were C can be ported. What is the reason for this ?
When the last full C++ spec came out in 1998, there was a long lag between then and the first fully conformant C++ compilers. Many implementations still ignore some of the dustier corners of the language (e.g. the export keyword) and in this way, C is more portable. This is especially the case with regards to embedded systems where g++ might not have been ported to a certain processor and one relies on Metrowerks Codewarrior for better or worse.

C is a very simple language so there is generally a full compiler for it for every processor available. However, this doesn't mean the compiler for your anti-lock brakes firmware is going to compile Quake. So the distinction between language portability is largely moot.

Share this post


Link to post
Share on other sites
Quote:
Original post by Quak
3. If not would it be faster to use a inline method e.g. for a
vector class vector.addvector(vector v) instead of v1 + v2 ?


Just a tiny point... and probably not consequential enough to worry too much about... but...

Functionally, v1.addvector(vector v2) and v1+v2 are very different beasts. The former alters the value of the vector v1 and is equivalent to v1+=v2. The latter does not alter the value of either vector but returns the result of the addition.

Cheers,

Timkin

Share this post


Link to post
Share on other sites
C++ is pretty cross platform (except for embedded systems and such), but 3rd party librarys may not be cross platform. So if your concerned about the app running on Windows, Mac, and Linux, you'll need to do some research before you can begin.

Share this post


Link to post
Share on other sites
edit: unborked

1. If you overload an operator does this work like a function
and creates the variables on the stack ?

Yes, but of course your compiler is allowed to make optimizations.

2. If so can you inline operator overloads ?

Yes, and for trivial operations your compiler will most likely inline them anyways.

3. If not...

Not applicable since #2 is "Yes".

4. I have heard C++ is not portable to another sytem in some
cases were C can be ported. What is the reason for this ?

Well, you should be able to compile a C++ compiler using the C compiler, but some systems only have very broken implementations of C or linking utilities that are very broken, resulting in much pain if you try to get a C++ compiler running. People being lazy, sometimes this means nobody will have done it for you on obscure platforms.

There's also embedded systems, where some of the overhead of C++ is insane for useage with them (example: you can easily add many kilobytes of program .exe size just from using the iostream library)

5. Should I try to avoid polymorphy and function pointers to
avoid a virtual table to make my code faster ?

No. They will make your code somewhat slower, but not any more so than any workarounds to the problem in many circumstances. Also, the slowdown is very minor - you should only go to the effort of avoiding it in a very few cases. Places to avoid virtual or pointers to functions do exist, such as:

The code for a vertex class, if it will be called every frame for every vertex (aka, Vertex.Draw() being overloaded to glVertex3f(...) or the DirectX equivilant).

In my above example, a more appropriate solution would be to make a higher level construct (such as a mesh object) be the point where the virtual function/choice occurs, then have that class contain an array of COpenGL_Vertex or CDirectX_Vertex depending on if the class was an COpenGL_Mesh or CDirectX_Mesh - meaning you get 1 virtual call per mesh per frame instead of per vertex per frame.

6. Once I use a virtual table does it matter to code speed how
many virtual classes I use?

No. Well, okay, technically, yes, since more classes in general means a larger .exe file size, which will slightly increase the load time of your application due to the speed of your hard drive - but nothing more trivial than that.

[Edited by - MaulingMonkey on October 19, 2004 12:18:39 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this