Inline for clone() function

Started by
13 comments, last by Alundra 10 years ago

What's about destructor ? Has it a problem about inline ?

If you want to delete the derived classes via an IBase*, then yes, it has the same "problem", as I said this applied to all virtual methods. You should really reread all the posts carefully though, you didn't seem to get the "don't manually inline because its a waste of time and doesn't do anything in most cases" most people where pointing at.

Advertisement

Every sane version of clone() would be a virtual method (or there would be no point having a clone() method) in which case, assuming you are calling through a base pointer, it can't be inlined anyway.

Ok the if a function is always called from base class (virtual) so it can't be inlined and must be in .cpp.
What's about destructor ? Has it a problem about inline ?

No, it's fine in the header. It just won't be inlined. But that doesn't matter. Let the compiler worry about that.

Does all that mean it's the end of inline with modern compiler ?

inline still have its' uses. Just not much for optimization

Consider if you are writing a header only library:


#pragma once
class bar;

class foo{
public:
	foo();
	bar *b;
};

class bar{
public:
	bar(foo *_f):
		f(f)
	{
	}
	foo *f;
};

inline foo::foo()
	:b(new bar(this))
{}


inline on foo::foo() is mandatory. it ensures the compiler and liker that no matter how many times it see the definition of foo::foo(), it will refer to a completely same function. Remove inline and you'll get a linker error because of multiple definitions.

At the end he is just there to give faster compile-time so.

This topic is closed to new replies.

Advertisement