473,796 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inline function in a class

How to make a member function inline while the declaration and
implementation are separated?
Sep 7 '05 #1
6 2411
kceiw wrote:
How to make a member function inline while the declaration and
implementation are separated?


If you want to keep the declaration and body separate, then the easiest
way to do it is by puttin the function body in the same header file as
the class definition, but outside the actual class.

E.G.:

--- BEGIN EXAMPLE.H ----
class Example {
public:
Example();
inline void helloWorld() const;
};

void Example::helloW orld() const {
std::cout << "hello world";
}
--- END EXAMPLE.H ---

Sep 7 '05 #2

Squeamizh wrote:
kceiw wrote:
How to make a member function inline while the declaration and
implementation are separated?


If you want to keep the declaration and body separate, then the easiest
way to do it is by puttin the function body in the same header file as
the class definition, but outside the actual class.

E.G.:

--- BEGIN EXAMPLE.H ----
class Example {
public:
Example();
inline void helloWorld() const;
};

void Example::helloW orld() const {
std::cout << "hello world";
}
--- END EXAMPLE.H ---


Your example wouldn't result in the member function being declared
inline, as the OP specified. I think perhaps you meant something like:

class Example {
public:
void memFunc() const;
};

inline void Example::memFun c() const
{
doSomething();
}

Best regards,

Tom

Sep 7 '05 #3
There is no way to completly force inline ....
(what if your function is recursive ? =) )

inline gives a hint (gcc will (normally?) respect this when given the
right compilation options. (don't recall - RHFM).

otherwise gcc will do the trick with __inline__.

MS has __forceinline (I have only read about this never used it.)

so you have to do something linke

#ifdef WIN32
// or maybe extent to win64 or make more macro ifs.
#define STRONGER_INLINE _HINT __forceinline
#else
#define STRONGER_INLINE _HINT __inline__
#endif

Well maybe STRONGER_INLINE _HINT is a bit long ...

/Thorbjørn

Sep 7 '05 #4
Thomas Tutone 写道:
Squeamizh wrote:
kceiw wrote:
How to make a member function inline while the declaration and
implementati on are separated?


If you want to keep the declaration and body separate, then the easiest
way to do it is by puttin the function body in the same header file as
the class definition, but outside the actual class.

E.G.:

--- BEGIN EXAMPLE.H ----
class Example {
public:
Example();
inline void helloWorld() const;
};

void Example::helloW orld() const {
std::cout << "hello world";
}
--- END EXAMPLE.H ---

Your example wouldn't result in the member function being declared
inline, as the OP specified. I think perhaps you meant something like:

class Example {
public:
void memFunc() const;
};

inline void Example::memFun c() const
{
doSomething();
}

Best regards,

Tom


How about when the inline function are put outside the header file? When
I try to do it, it make an error while linking.
Do I have to put the function body in the header file when I want to
make it inline? Dose it differ when I define the function while it's
declared?
Sep 7 '05 #5
2 possibilities:

1) Then it is (probably) not inlined.
The function is however in 2 more objectfiles.

2) You implemented the function inline in
file1.cpp
and
file2.cpp uses the inline function, but how
should file2.cpp see a function that should be inline and exists in
file1.cpp

gcc has an option to force export of inlined functions, but ofcourse
they
will then NOT be inlined in file2.cpp

Sep 7 '05 #6
<scissors applied>

How about when the inline function are put outside the header file? When
I try to do it, it make an error while linking.
Do I have to put the function body in the header file when I want to
make it inline? Dose it differ when I define the function while it's
declared?


As long as the declaration is visible when you call the function, you
should not get a linker error.

It does not make a differnce if you have separate definition/declaration
from a declaration/definition in one place. However, if the
_definition_ is not visible when you call the function, there is high
chance that the function will in fact not be inlined (This is very
difficult for the compiler/linker).

GAbriel
Sep 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
6572
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg. accessor and mutator methods to access private data members). I would like to know if there are any issues with using inline functions that may have attributed to my errors before I start separting them out into "outline functions". Regards
20
3157
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
7
2863
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must be defined either within the class or within the same header file. But its generally a good programming practice to have the declarations and definitions in seperate files. This would make the future maintenance of the code easier.
6
4009
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
5
1930
by: Bert Jansen | last post by:
There seems to be a bug in de VS .net C++ compiler (optimization) when using inline functions that return static data. The following code demonstrates this (Win32 console app with ATL support): #include "stdafx.h" #include <atltime.h> int _tmain(int argc, _TCHAR* argv) {
7
16127
by: Wu Shaohua | last post by:
Hi Guys, 1. As we know usually we should not define a constructor as inline. I also learned if we define a member function inside the class this member function will be automatically be inline'ed. My question is: If I define a constructor (including its body) or another large member function inside the class, the constructor or the member function is inline or not? why? 2. I learned that if the member function is big we should not...
25
2031
by: toton | last post by:
Hi, As inline is not mandetory, it depends on compiler to inline certain function (or using switch like fior GCC), my question is there any scope for inlining when it is not declared as inline function? i.e compiler may choose not to inline certain inline function, but is it free to choose a non inline function to inline it? I have some simple one line get function, and index operators which I want to get inlined. But due to some problem...
2
3778
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
17
8386
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.