Refereces vs Pointers on the C++ code.

Thiago Macieira thiago at macieira.org
Fri Apr 12 13:14:02 PDT 2013


On sexta-feira, 12 de abril de 2013 12.11.32, Tomaz Canabrava wrote:
> Hello Dears :)
> 
> I was ausent, sorry for that, my health is not very good this days.

Hello Tomaz

Please don't feel pressured from us. We'd rather keep you for the long haul, 
rather than see you burn out.

> 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.

Not really. You can do crazy things like:

	Foo &ref = *new Foo;
	delete &ref;

Hopefully, not many people will do that. In general, I agree that objects 
allocated on the heap should be addressed by a pointer.

Also, let's make a distinction between plain references and const-references. 
Const references behave almost identically to a plain object. The Qt meta 
object system even converts a const-reference argument in a signal or slot to 
its plain value, for simplification.

Passing by value or by const reference is a slightly complicated subject. You 
can read more in my blog:
	http://www.macieira.org/blog/2012/02/the-value-of-passing-by-value/
(I've been asked to write the sequels "Pointers to passing by pointer" and 
"Reference guide in passing by reference")

> 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. )

Before we go there, let me explain another aspect of Qt programming:

	value-type classes vs object-type classes

The vast majority of Qt classes fall into one of those two categories. Their 
attributes are:

Object-type classes:
	* derive from QObject
	* are not copyable
	* have thread affinity
	* obey the parent-child relationship
	* are often allocated on the heap, using "new"
	* are passed by pointers

Value-type:
	* (often) have no base classes
	* (most often) are reference-counted, implicitly-shared with atomic COW
	* are copyable and movable
	* do not have parents or children
	* are often allocated on the stack or direct as members of other classes
	* are passed by const-refs and returned by value
	* should be treated like you'd treat an "int"

So, you should write:
	void some_func(QString *s);
if you'd write under similar circumstances:
	void some_func(int *i);

Where you'd write:
	void some_func(int i);
you should use:
	void some_func(const QString &s);

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20130412/20810f03/attachment.sig>


More information about the subsurface mailing list