Re: C programmer migrating to C++
On 25 Jan., 13:50, Bartosz Wiklak <bwik...@gmail.com> 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 that this argument (and part of function's job) is not
relevant. I know this could be considered unsafe but imagine several
function calls inside of such function that also use NULL pointer. It
saves much work. In STL there are some values that represent bad or
boundary values such as string::npos or the one returned by iterator
end() method ( btw, it's irritating that every container has different
past-the-end element). Can I achieve something like default NULL
argument in C++ not using pointers but references?
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"); ]
BTW, is [ string s("comp.lang.c++.moderated"); ] faster than [ string
s ="comp.lang.c++.moderated"; ] or maybe it is compiler dependent?
Thanks,
Bartek
--
[ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
1) In fact, References ARE pointers, designed not to contain NULL
pointers. They are like "aliases". Internally, they're handled like
pointers, but get automatically dereferenced. So you cannot pass NULL
in a reference. Use the pointers for such things, or consider using
shared_ptr.
2) Do NOT return references to local variables. As explained before,
references are special pointers, and the local object gets destroyed
when you return, thus the reference gets invalid (what is very
difficult to debug and makes headaches...). By the way, an
initialization of an object with "=" on the same line as the
declaration calls the constructor, not the assignment operator. Thus
'string s="test";' and 'string s("test");' are totally equivalent.
Also, normally most classes have implicit constructors, so you could
write 'string foo(){ return "bar"; }'. Most compilers should not
create such a function as long as you do not create a pointer to it.
Or try 'inline':
-- inline string foo() { return "bar"; }
-- string s = foo();
would become
-- string s = "bar";
and this would be
-- string s("bar");
I hope I could help you, as I haven't much time yet.
Kind regards, Syron
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]