Jump to content
  • Advertisement
Sign in to follow this  
Quak

Questions on C++

This topic is 4991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!