Refereces vs Pointers on the C++ code.
Tomaz Canabrava
tcanabrava at kde.org
Fri Apr 12 08:11:32 PDT 2013
Hello Dears :)
I was ausent, sorry for that, my health is not very good this days.
But I was reading another thread where the `c++ references vs c style
pointers` was starting to be discussed, so I tougth it was a good idea to
come here and talk a bit about them before we decide, since the majority of
the subsurface programmers are non-c++ developers, I`ll try to focus on
pratical terms. I do not want to `sell` c++ over c, to make that clear.
1 - with references you can know what`s declared on the stack over the heap.
We know that memory management is sometimes a pain, and sometimes a
function allocates memory for you to manage it. now, if we got a function
named `process_events(const SubsurfaceEvents &events)`, we know that the
code inside that `process_events` will not:
1 - attemp to modify the events ( because of the const-qualifier )
2 - attemp to delete the events
if the function was declared by process_events(SubsurfaceEvents *events) in
the other hand, we can pass a pointer to it, and this pointer could be
freed inside of the function. if we passed a stack-variable by it`s
pointer, we could get a crash.
2 - With references we don`t loose the class-semantics. In c++ one can
override an operator ( +, -, and such ). While I know that a lot of
programmers don`t like to use operator overloading, they work over the
object, and not the pointer.
for example:
QString s = projectFile + ' - ' + activeTabBar;
window.setTitle ( s );
with projectfile and activeTabBar as `QString`
if they where a QString* , this would rather be:
QString s = (*projectFile) + ' - ' + (*projectFile)
needing a 'variable-of-address' operator for each pointer that we have.
So,
This is what I know about the diferences about References and Pointers, and
I`ll use the one Dirk and the team chooses to ( except the ones that the Qt
lib forces me to use. )
Tomaz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20130412/e4a14ce6/attachment.html>
More information about the subsurface
mailing list