Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

C++ : Are inline virtual destructors an optimization?

This topic is 4842 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 just reviewed a web-site on Functionoids, which advised that inline destructors for virtual classes are faster. The code was something like this:
class Foo
{
   virtual ~Foo();
   virtual void Bar(int) = 0;
};

inline Foo::~Foo()
{ }
If so, why? And should all virtual destructors be inlined when speed is important? --random

Share this post


Link to post
Share on other sites
Advertisement
Base class destructors are automatically called by the derived class destructors. In that sense virtual destructors are different than normal virtual functions. By inlining a virtual destructor a derived class will have access to the definition of the base class destructor and thus inline the base class destructor, which will eliminate the call overhead of calling the virtual destructor. As usual, I recommend only inlining if your profiler tells you there's a bottleneck or if it is necessary (like for template classes).

Share this post


Link to post
Share on other sites
I suppose that the speed gain is very very very marginal and probaly, if your destructor does something, you have no benefit at all from inline it!

Share this post


Link to post
Share on other sites
Thanks Si,

That's very interesting, and explains their advice excellently. I suppose that Functionoids, which can be used in place of function pointers in C++ (and are more powerful) would be called frequently (often within a loop) which would make this inline call worthwhile.

I've just used this concept to write a text parser (state machine) which works superbly, and is very easy to maintain. Using functionoids, it is possible to produce a completely encapsulated parser, that can be called in code like this:


void Database::parse( const string data_file, const string state_file )
{
char ch;
ifstream in(data_file);
ofstream out(state_file);

FSMAction filter; // FSM machine functionoid, encapsulated and polymorphic w/ctor args.

while (in.get(ch))
{
filter(ch,out);
}
}


Where the FSMAction class contains a method like this:


void FSMAction::operator()(char ch,ostream & out)
{
pVec[object_state_]->action(ch,out,object_state_); // filters ch and resets object_state_.
}


C++, what a brilliant programming language!

--random.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!