Jump to content
  • Advertisement
Sign in to follow this  
MajinMusashi

Problem inlining methods in C++

This topic is 5042 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

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
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.

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
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!