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

Home Posts Topics Members FAQ

Use Inline in class?

I want to know if the practice is the best. Do I need to place inline
keyword inside class definition or outside member function
definition. For example

class A
{
public:
A();
~A();

inline void Test(); // should place inline here?
};

inline void A::Test() // should place inline here?
{
{
Jul 6 '08 #1
32 2090
HL
On 7ÔÂ6ÈÕ, ÉÏÎç8ʱ31·Ö, Immortal Nephi <Immortal_Ne... .@satx.rr.comwr ote:
I want to know if the practice is the best. Do I need to place inline
keyword inside class definition or outside member function
definition. For example

class A
{
public:
A();
~A();

inline void Test(); // should place inline here?
no need
};

inline void A::Test() // should place inline here?
{
{
yes, you need.

another way is to define func in class.
class A{
public:
void Test(){...};
};
Test is refered as inline.

Jul 6 '08 #2
Sam
Immortal Nephi writes:
I want to know if the practice is the best. Do I need to place inline
keyword inside class definition or outside member function
definition. For example

class A
{
public:
A();
~A();

inline void Test(); // should place inline here?
No.
};

inline void A::Test() // should place inline here?
Yes.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkh wGSoACgkQx9p3GY HlUOI+NQCbBuUnX a7tCUMjpcGCPo9Y KvbA
yFwAn271esMxKOB acxrSFw31D46g17 m3
=TrHL
-----END PGP SIGNATURE-----

Jul 6 '08 #3
Immortal Nephi wrote:
I want to know if the practice is the best. Do I need to place inline
keyword inside class definition or outside member function
definition. For example

class A
{
public:
A();
~A();

inline void Test(); // should place inline here?
};

inline void A::Test() // should place inline here?
{
{
If you want to define your inline member function outside the class
definition (as in your example above), you only need to specify 'inline'
in the member definition (the second 'inline' in your example)

class A {
...
void Test();
...
};

inline void A::Test()
{
}

--
Best regards,
Andrey Tarasevich
Jul 6 '08 #4
Andrey Tarasevich wrote:
If you want to define your inline member function outside the class
definition (as in your example above), you only need to specify 'inline'
in the member definition (the second 'inline' in your example)
If this is the case, why is the keyword 'inline' even supported inside
class definitions? Isn't it completely obsolete and a no-op there?
(Basically there's not even one single case where specifying that
keyword in the class definition makes a difference.)
Jul 6 '08 #5
On Jul 6, 10:30 am, Juha Nieminen <nos...@thanks. invalidwrote:
Andrey Tarasevich wrote:
If you want to define your inline member function outside
the class definition (as in your example above), you only
need to specify 'inline' in the member definition (the
second 'inline' in your example)
If this is the case, why is the keyword 'inline' even
supported inside class definitions? Isn't it completely
obsolete and a no-op there? (Basically there's not even one
single case where specifying that keyword in the class
definition makes a difference.)
You can specify inline on either the definition or any
declaration which precedes it, and the function will be inline.
Current practice is to specify it on the definition, but at
least one earlier compiler I used required it to be specified on
the first declaration that appeared.

Of course, current best pratice is not to use inline at all, so
the problem doesn't occur.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 6 '08 #6
Of course, current best practice is not to use inline at all, so
the problem doesn't occur.
Why do you say this? In-lining functions in a header file can be
critical for performance reasons.
Thanks,
Joe Cook
Jul 6 '08 #7
On Jul 6, 5:20 pm, joseph cook <joec...@gmail. comwrote:
Of course, current best practice is not to use inline at all, so
the problem doesn't occur.
Why do you say this?
Because it increases coupling.
In-lining functions in a header file can be critical for
performance reasons.
Really? I never use it, and I've not had any performance
problems.

If you do actually have a performance problem, and the profiler
shows that it is in a tight loop where you do call a non-inlined
function, then inlining it is a simple and cheap optimization.
Using inline before the profiler says you have to, however, is
premature optimization.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 6 '08 #8
Alf P. Steinbach wrote:
* James Kanze:
[...]
>If you do actually have a performance problem, and the profiler
shows that it is in a tight loop where you do call a non-inlined
function, then inlining it is a simple and cheap optimization.
Using inline before the profiler says you have to, however, is
premature optimization.

Inlining things in headers is a technique that saves programmer's time
both for initial development, for use of the header, and for
maintainance (which reportedly constitutes about 80% of all programming
work).
I think that if this is really your issue (hardly, but I'll assume you
have some measurements) then you (or your users in the case e.g. of
libraries, for which I've often heard the "header-only is easier"
claim) are likely to work with a severely limited environment and
toolset. To me, creating one more file is just a matter of issuing ":e
filename" and the file contents get initialized automatically with all
that can be written automatically. Similarly for building. As to
maintenance I really don't understand: what time do you save?

--
Gennaro Prota | <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Jul 6 '08 #9
On Jul 6, 5:59 pm, "Alf P. Steinbach" <al...@start.no wrote:
* James Kanze:
On Jul 6, 5:20 pm, joseph cook <joec...@gmail. comwrote:
>Of course, current best practice is not to use inline at all, so
the problem doesn't occur.
Why do you say this?
Because it increases coupling.
In-lining functions in a header file can be critical for
performance reasons.
Really? I never use it, and I've not had any performance
problems.
If you do actually have a performance problem, and the profiler
shows that it is in a tight loop where you do call a non-inlined
function, then inlining it is a simple and cheap optimization.
Using inline before the profiler says you have to, however, is
premature optimization.
Inlining things in headers is a technique that saves
programmer's time both for initial development, for use of the
header, and for maintainance (which reportedly constitutes
about 80% of all programming work).
I don't know where you get that from. It is a nightmare with
regards to maintenance, easily doubling the effort required.
(To begin with, if you don't know if the function is inlined or
not, you don't even know in which file to look for it. And of
course, if you have to modify it, then all of the client code
needs to be recompiled.)
I agree that doing inlining for reasons of performance would
be premature optimization, of the kind that might even
influence runtime in a negative way, and bring in unwanted
coupling and have other undesirable effect.
However, used as a means for programmer productivity, not as
premature optimization, its effect on coupling is generally
negligible.
If it's used in a template, it's negligible, because the
template code has to be included anyway (at least with most
current compilers). If it's not a template, however, the effect
in practice can be quite high.

Obviously, it's only one factor: you can write impossible to
maintain code without a single inline function, and carefully
used, you can minimize the impact. But globally, all of the
coding guidelines I've seen for application code forbid inline
functions---and usually also require user defined
implementations of the functions the compiler will generate by
default as well, since the compiler generated versions are
inline. (The rules for library code are somewhat different,
since you often have to guess where the client code's
bottlenecks might occur.)
It can even reduce the number of files that must be opened and
read during a build. I think the programmer who's afraid of
using std::vector just because it's perceived as a large
header & requires full def of element type, is seriously
misguided.
That, certainly. But std::vector is (hopefully) stable code,
which isn't evolving while you're working. So you won't end up
having to recompile everything because there was a small
correction in the implementation somewhere. (If you upgrade
your compiler, of course, you'll get a new version of
std::vector. But if you upgrade your compiler, you have to
recompile everything anyway, since all of the object files
depend in some way on the compiler. On the other hand, you
shouldn't be upgrading the compiler very often---maybe once
every two or three years, at the most.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 7 '08 #10

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

Similar topics

2
10119
by: Jeff Williams | last post by:
The common method of defining template classes and functions is to put the definition and declaration into the same header file. Or at least I believe it to be the common method and it is certainly the one I use. This leaves me with a question. As example, consider the following class. template<class T> class foo
23
3162
by: Mat | last post by:
<div id="container"> <div id="main"> <div id="header"> <p class="Address">123 Fake Street, </p> <p class="City">Crazy City, </p> <p class="Province">Ontario </p> <p class="PostalCode">H0H 0H0</p> <p class="Telephone">Telephone: 555-1234 </p> <p class="Fax">Fax: 555-4321</p> </div>
20
3155
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.
7
2036
by: Alvin | last post by:
Hello all, I'm curious as to your opinions on explicitly inlining function? I'm talking about functions as members of a class. For example, so class A defines a operator==() and a operator!=(): class_a.h: class A {
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
7
16125
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...
5
1923
by: Barry | last post by:
Hi, group First, I write same cases I've already known, I don't concern that specific compiler really do inline or not. Please check them if they are right, and add the cases I miss 1. // a.h inline void f() {}
2
1557
by: Barry | last post by:
Hi, group First, I write same cases I've already known, I don't concern that specific compiler really do inline or not. Please check them if they are right, and add the cases I miss 1. // a.h inline void f() {}
17
8384
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
9645
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
10336
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...
1
10095
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
9953
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...
1
7502
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...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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.