473,397 Members | 1,969 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,397 software developers and data experts.

inline constructor and destructor

Tim
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

Sep 12 '07 #1
5 8496
Tim wrote:
I am confused about when I should use inline constructors and
destructors.
In most cases if your function does not need to use any type that
depends on this type to be complete, and the function seems to fit
in a few lines of code, there is nothing to stop you from defining
it in the class definition.
I have test some simple cases, seems the inline ones are
faster.
Good. Keep them.
Why some book suggest not use/overuse the inline constructors
and destructors?
What book?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '07 #2

"Tim" <Ti************@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
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?
Maybe some insight here:
http://www.codeguru.com/cpp/tic/tic0162.shtml
Sep 12 '07 #3
James Kanze wrote:
>
<good advice snipped>
If a book emphasizes not inlining constructors and destructors,
it is probably because in these cases, even apparently trivial
or empty functions often contain a lot of implicit, compiler
generated code, and can end up quite large, and result in
significant code bloat. (This is probably less of an issue than
it used to be, but it is still worth considering; even if code
bloat typically won't prevent the code from fitting in memory,
it will reduce locality, and increase cache misses, and possibly
even cause more page faults.)
While I agree with most of this (and everything I snipped), also note
that compiler/linker combinations are also getting good at eliminating
duplicated code, so an inline constructor will only appear once in the
final executable.

--
Ian Collins.
Sep 13 '07 #4
On Sep 13, 10:13 am, Ian Collins <ian-n...@hotmail.comwrote:
James Kanze wrote:
<good advice snipped>
If a book emphasizes not inlining constructors and destructors,
it is probably because in these cases, even apparently trivial
or empty functions often contain a lot of implicit, compiler
generated code, and can end up quite large, and result in
significant code bloat. (This is probably less of an issue than
it used to be, but it is still worth considering; even if code
bloat typically won't prevent the code from fitting in memory,
it will reduce locality, and increase cache misses, and possibly
even cause more page faults.)
While I agree with most of this (and everything I snipped), also note
that compiler/linker combinations are also getting good at eliminating
duplicated code, so an inline constructor will only appear once in the
final executable.
If they're not inlined. The whole point of inlining is that the
code will appear in place of the call.

Globally, of course, this is much less of a consideration than
it used to be. Almost all of the coding guidelines I saw in the
1990's forbid inline for constructors and destructors, for this
reason. More recent ones don't, except in so far as they forbid
inline in general. (My own rule would be to forbid inline in an
"exported" file, i.e. a header which will be used by the
client. With more tolerance in the case of templates.)

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

Sep 14 '07 #5
James Kanze wrote:
On Sep 13, 10:13 am, Ian Collins <ian-n...@hotmail.comwrote:
>While I agree with most of this (and everything I snipped), also note
that compiler/linker combinations are also getting good at eliminating
duplicated code, so an inline constructor will only appear once in the
final executable.

If they're not inlined. The whole point of inlining is that the
code will appear in place of the call.
True, but even if they are, some compilers can factor out repeated code
as a space optimisation.

--
Ian Collins.
Sep 14 '07 #6

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

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: JAL | last post by:
According to MSDN2, if a managed class throws an exception in the constructor, the destructor will be called. If an exception is thrown in the constructor the object never existed so how in the...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
7
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...
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
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.