# Problem inlining methods in C++

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

## Recommended Posts

Interesting question (at least to me :) ). I like to keep the classes in my C++ project somewhat like Java (with just one class per file), and a header file to that class: wheel.hpp
#ifndef _WHEEL_HPP_
#define _WHEEL_HPP_

class C_Wheel {
private:
bool isEnabled;
public:
bool getIsEnabled();
};

#endif


wheel.cpp
#include "wheel.hpp"

// ctors and dtor
bool C_Wheel::getIsEnabled() {
return isEnabled;
}


This raises two questions: 1- Is this a good pratice? What is the pros and cons of such approach? 2- As I don't want to declare nothing in the headers (methods are inlined by default), how do I inline methods declared in the cpp file? Is it possible? I've read that YES (http://www.cs.utexas.edu/users/downing/cs378/Elements/Methods.html), using "inline boll C_MyClass::getIsEnabled()" in the cpp, but Visual C++ 6 drops a "error LNK2001: unresolved external symbol 'public: void __thiscall C_Wheel::roll(float)'". Thanks!!

##### Share on other sites
Thats how most commercial games i've seen done it. Look at the Half Life 2 SDK.( Half Life 1 works as well)

They sometimes implement the classes in the header but rarely.

##### Share on other sites
VC6 is not an ISO compliant C++ compiler. You will have to declare all your inline methods in the header. You can 'cheat' a little by seperating them into different files, though (common practise):
// header:#ifndef _WHEEL_HPP_#define _WHEEL_HPP_class C_Wheel {private:	bool isEnabled;public:	bool getIsEnabled() const;	};#include "wheel.inl"#endif

// wheel.inl:// ctors and dtorinline bool C_Wheel::getIsEnabled() const {    return isEnabled;}

Sidenote: 'getIsEnabled()' is not a good method name. 'IsEnabled()' is sufficient.

##### Share on other sites
Side note: Rampant inlining of everything is not necessarily a good thing.

##### Share on other sites
Quote:
 Original post by SumDudeThats how most commercial games i've seen done it. Look at the Half Life 2 SDK.( Half Life 1 works as well) They sometimes implement the classes in the header but rarely.

I'm in the right way :)

##### Share on other sites
Quote:
 Original post by MiserableSide note: Rampant inlining of everything is not necessarily a good thing.

Yet it is not a Bad Thing™ esp. for such accessor methods.
'inline' is just a hint to the compiler, anyway...

##### Share on other sites
Quote:
 Original post by darookieVC6 is not an ISO compliant C++ compiler. You will have to declare all your inline methods in the header. You can 'cheat' a little by seperating them into different files, though (common practise):

Very interesting! It's a so used tech, yet not supported by MSVC++...
Ah, your cheat is very creative although the code itself is not so beatiful this way [wink]

Quote:
 Sidenote: 'getIsEnabled()' is not a good method name. 'IsEnabled()' is sufficient.

Agreed! Certainly a bad example :)

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999487
×