Problem inlining methods in C++

Started by
5 comments, last by MajinMusashi 19 years, 4 months ago
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!!
Advertisement
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.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
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.
Side note: Rampant inlining of everything is not necessarily a good thing.
Quote:Original post by SumDude
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.

I'm in the right way :)
Quote:Original post by Miserable
Side 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...
Quote:Original post by darookie
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):

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 :)

This topic is closed to new replies.

Advertisement