[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: object-model: Return by value, reference or pointer? (or something else?)

From: Branko Čibej <brane_at_xbc.nu>
Date: Tue, 12 Oct 2010 21:40:23 +0200

 On 12.10.2010 20:35, Hyrum K. Wright wrote:
> 1) Return everything by value
> Pros: simpler memory management, less overhead (?)
> Cons: doesn't allow the return of NULL values, need to establish
> conventions to represent NULL objects (an isNull() method?)

Meh.

    inline operator bool() const { return (this->c_struct_pointer != 0); }

> 2) Return everything by pointer
> Pros: can represent the NULL value, potentially less memory overhead
> Cons: more complex memory management (caller must delete returned value)

This would be seriously horrible since any user of the library would
then have to invent their own way to manage the lifetime of these
objects (i.e., wrap them in smart pointers). Defeats the whole purpose
of having a C++ API in the first place IMHO.

> 3) Some combination of (1) & (2)
> Pros: Can return guaranteed existing things by value, potentially
> NULL ones by pointer
> Cons: Making the distinction between optional and required takes
> more effort, a heterogeneous API is more work for callers

Yup, so it is, so go with (1) and do it right. :)

-- Brane
Received on 2010-10-12 21:41:13 CEST

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.