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

Home Posts Topics Members FAQ

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 16099
* 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.c om> wrote in message
news:e2******** **@news.cn99.co m
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********@com cast.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********@com cast.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********@com cast.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
3852
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
4
2368
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
2435
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 external files contains a class A. The inline JS then inherits a class B from A. My question is now, since the class A is defined in the external file, is the class A definitely known at the point where I use it? I could
5
1670
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: ============================================== John Torjo wrote: >
14
1847
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 js interpreter has to look through all the method definitions (did a profile using Venkman) which takes too much time for me .. would it make a difference if i define these methods outside the constructor via .prototype (see "Version Prototype")...
9
1835
by: 2005 | last post by:
Hi If I have a class, class CNode { public: CarNode() : m_pNext(0), m_ticketNum(0) {} ~CarNode(); private:
2
2738
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 why? I mean, whats the difference between by value and reference arguments wrt inlining?
2
1822
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 inline methods of the library classes from my managed classes, the inlining does not seem to occur, and I end up with a linker error 2001 - unresolved external. Is there a trick for getting this to work. I have tried using Default inline (/Ob0) in...
5
8507
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 inline constructors and destructors? Thanks, Tim
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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
8503
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
7324
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...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
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.