473,418 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Inline Constructor

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 define it as
inline. Can anyone tell me how large that a member function should not be
inline? I suppose it depends on different compiler. I want to know clearly
when I should use inline and when should not.

Thanks in advance!

Sincerely
Wu
Apr 18 '06 #1
7 16043
* Wu Shaohua:

1. As we know usually we should not define a constructor as inline.
I don't know that; it must be something new that I'm not aware of...

I also
learned if we define a member function inside the class this member function
will be automatically be inline'ed.
No, it won't. It's the same as declaring it 'inline'. Which does not
guarantee inlining.

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?
Any member function definition inside the class definition is equivalent
to an 'inline'-declared definition outside the class. One possible
reason it is like that is the usual physical packaging of C++ code,
where class definitions are provided in header files that are physically
included by other files. So without the rule you'd violate the one
definition rule; what 'inline' tells the compiler is that you have
ensured that all definitions (which come from including that header) are
identical, not in conflict with each other.

2. I learned that if the member function is big we should not define it as
inline. Can anyone tell me how large that a member function should not be
inline? I suppose it depends on different compiler. I want to know clearly
when I should use inline and when should not.


You should use member function definitions inside the class definition
when that makes the code more clear.

You should provide an 'inline' external definition when that makes the
code (or e.g. the build process) more clear.

To influence actual /inlining of the machine code/, which is something
else entirely, you have to use compiler-specific means, and anyway, as a
novice you shouldn't even think about it: "premature optimization is the
root of all evil", and regarding inlining it will be pure chance if you
manage to do better than the compiler would without interference.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 18 '06 #2
"Wu Shaohua" <fl****@gmail.com> wrote in message
news:e2**********@news.cn99.com
Hi Guys,

1. As we know usually we should not define a constructor as inline.
I don't know that.
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?
Yes. Because the language says so. Note that the function is inline in the
same sense as a function is inline when you explicitly use the inline
keyword. This is a hint to the compiler which may or may not actually inline
the function. Further, some compilers will inline functions even when they
are defined outside the class declaration and even though the inline keyword
is not used.
2. I learned that if the member function is big we should not define
it as inline. Can anyone tell me how large that a member function
should not be inline? I suppose it depends on different compiler. I
want to know clearly when I should use inline and when should not.


I think you hope for too much.
--
John Carson

Apr 18 '06 #3
Wu Shaohua wrote:
1. As we know usually we should not define a constructor as inline.
Why?
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?
"inline" means two things. It means the 'inline' keyword, which instructs
the compiler to neglect the "one definition rule" for a function body, and
to permit that function body (or constructor or destructor) to appear in
more than one translation unit. That's generally the sum of one .cpp file
and all its #included .h files (under common naming conventions).

Programmers say "inline" to also mean "inlined opcodes". A function's
opcodes get inserted into the calling function's opcodes, without a
hardware function call between them.

Blocks of code (functions, constructors, and destructors) inside classes (or
structs) automatically get an implied 'inline' keyword.

The keyword is only a hint to the compiler to inline a function's opcodes.
The compiler can inline the opcodes of functions not declared inline, and
can out-of-line functions declared inline.

Neither situation is automatically faster, and you will do better to write
clean code and let the compiler sort such issues out.
2. I learned that if the member function is big we should not define it
as
inline. Can anyone tell me how large that a member function should not be
inline? I suppose it depends on different compiler. I want to know
clearly when I should use inline and when should not.


Functions should not be large.

Now to answer my "Why?" up above. If you write "lots of stuff" into a .h
file, such as long function bodies, or extra #include statements, then the
odds increase that you will change those contents as your program grows.
This will cause cascading recompiles as clients of your .h file recompile,
even when you change things they don't use.

..h file should be as lean as possible, with all function bodies moved out
to .cpp files. The .h file should #include the fewest possible other .h
files, and it should forward-declare classes that it uses, if possible:

class Foo;

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 18 '06 #4
"Wu Shaohua" writes:
1. As we know usually we should not define a constructor as inline.


Nonsense.
Apr 18 '06 #5

"osmium" <r1********@comcast.net> wrote in message
news:4a************@individual.net...
| "Wu Shaohua" writes:
|
| > 1. As we know usually we should not define a constructor as inline.
|
| Nonsense.
|
|

Sorry, I didn't make it clear. I meant usually a contructor has lots of
things to do such as invoking the base class's contructor, initializing
member data etc. In this case, the constructor will be complex. So we
should not use inline for such a constructor. Is it correct?

Actually, what I am interested in is how the compiler control the inline
stuff.

And thank all you guys for answering my question.
Apr 18 '06 #6
Wu Shaohua wrote:
"osmium" <r1********@comcast.net> wrote in message
news:4a************@individual.net...
| "Wu Shaohua" writes:
|
| > 1. As we know usually we should not define a constructor as inline.
|
| Nonsense.
|
|

Sorry, I didn't make it clear. I meant usually a contructor has lots of
things to do such as invoking the base class's contructor, initializing
member data etc. In this case, the constructor will be complex. So we
should not use inline for such a constructor. Is it correct?
Not necessarily. Write the code so that it is clear, and let the
compiler sort out what to do with the machine level stuff (like whether
the function is actually inlined or not). Only when you determine that
there actually is some sort of performance issue (by _measuring_, not
guessing), then worry about forcing the compiler to go one way or another.
Actually, what I am interested in is how the compiler control the inline
stuff.


That's compiler and platform specific stuff. If you check your
documentation, it probably has some mention of when a function will and
won't be inlined (and probably compiler switches and stuff to control it).
Apr 18 '06 #7

Wu Shaohua wrote:
"osmium" <r1********@comcast.net> wrote in message
news:4a************@individual.net...
| "Wu Shaohua" writes:
|
| > 1. As we know usually we should not define a constructor as inline.
|
| Nonsense.
|
|

Sorry, I didn't make it clear. I meant usually a contructor has lots of
things to do such as invoking the base class's contructor, initializing
member data etc. In this case, the constructor will be complex. So we
should not use inline for such a constructor. Is it correct?
The answer depends, your compiler will make the choice.
Actually, what I am interested in is how the compiler control the inline
stuff.

the keyword 'inline' is only a hint to a compiler, the compiler can
choose either to honor it or ignore it depending on compiler
heuristics. Some compiler vendor has vendor specific keyword to force
function inline but this practice is not standard.
And thank all you guys for answering my question.


no problem. You may want to find a good book that systematically
explains these things to you instead of relying on tid-bit information
gathered from a news group.

Apr 20 '06 #8

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

Similar topics

47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
4
by: sam | last post by:
Hi, I can't figure out what is the problem of the following coding. #ifndef __PARSER__ #define __PARSER__ #include <iostream> #include <string> #include <fstream> //#include <exception>
3
by: Juergen Stein | last post by:
Hi Group, I couldn't find an answer on this with Google, so let me test you :) I've a fairly complex WebApp, and I put most of the JS code in independent external .js files. One of these...
5
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
14
by: Gerald S | last post by:
hi, got a performance problem; situation is as described below (see "Version Inline"), i've got a javascript class item with many methods defined inside and with every call to "new item()" the...
9
by: 2005 | last post by:
Hi If I have a class, class CNode { public: CarNode() : m_pNext(0), m_ticketNum(0) {} ~CarNode(); private:
2
by: bob | last post by:
Hi, when compiling source with borland c++ builder, the compiler issues the following warning.... "functions taking class by-value arguments are not expanded inline" can anybody tell me...
2
by: =?Utf-8?B?TWljayBPJ05laWxs?= | last post by:
I am currently trying to wrap an old library (ImageMagick) in .NET, and am having problems with inline expansions. I have recompiled the library in vc++ 2005 OK. However, when I try to access any...
5
by: Tim | last post by:
Dear All, I am confused about when I should use inline constructors and destructors. I have test some simple cases, seems the inline ones are faster. Why some book suggest not use/overuse the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...
0
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...

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.