Sign in to follow this  
MajinMusashi

Problem inlining methods in C++

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 this post


Link to post
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 this post


Link to post
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 dtor
inline bool C_Wheel::getIsEnabled() const {
return isEnabled;
}



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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this