Re: OT - How Important is Performance In Modern C++ Development?
On Oct 29, 10:14 am, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
I have been living in the world of C# for the past 3 years. I have
recently been diving back into C++. In order to get my mind back in
the game, I picked up my copy of Scott Meyers' Effective C++. I have
been surprised just how much he seems to emphasize performance.
So, is performance that important in the C++ programming community?
It depends on the task. If you write an OS kernel, you probably should
do this with performance aspect in mind. If you write programm that
sorts array of 40 numbers, you can skip the performance issues.
Why did Mr. Meyers write over a page explaining that returning by
value is essentially necessary? What is a call to an extra copy
constructor?
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.9 explains it
better than I can do.
I'm also a little confused about the pImpl thing, too. So what if Joe
can see your implementation details? Who cares? or does it have
something to do with versioning?
If Joe knows about implementation details he can write code using this
knowledge. Later, when you change the implementation, Joe's code will
be invalid because of difference in implementations. So it's better to
hide your implementation away from the others.
It seems like C++ avoids the heap like the plague. Instead of
returning a vector<T>* from a method, the typical convention appears
to be to pass the vection pass-by-reference-to-const. This means
instantiating it in the caller and passing it. In C# the convention is
to typically build the collection inside and return it. Of course in
C# the heap is unavoidable in a lot of cases. Am I crazy or did I hit
it on the nose?
[If I understand you question rigth] It's because of memory
management. In C# (as in any .NET language) we have a garbage
collector, so you can easily create the collection instance inside the
collection builder and return pointer to it. The garbage collector
will "kill" this instance for you. In C++ if you allocates memory
(say, inside of collection builder) you need to track your collection
instance to prevent memory leaks. Just a little exmpl:
Collection* Build() {
Collection *c = new Collecion();
// ...
return c;
}
// C#
void foo() {
Collection *c = Build();
// do smthng
} // That's ok. Garbage collector will kill unneeded instance of the
Collection (c)
// C++
void foo() {
Collection *c = Build();
// do smthng
} // Ooops. Memory leak. You forgot to free memory.
// One possible solution (C++)
void Build(Collection &c) { // pass-by-reference
// do smthng with c
}
void foo() {
Collection c;
Build(c);
// ...
} // That's ok.
Well, just thought I would ask. The two languages may share a lot of
syntax in common but the conventions seem to be polar opposites.
Thanks,
Travis
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]