Const functions

I’ve been doing a bit of work, as suggested by Armando, in trying to
const’ify the libavogadro library. It looks like I’m hitting a brick
wall when i eventually get to functions like GetVector() and
GetAtomicNum() which most definitely should be constant functions.

However, the problem arises when dealing with GetBeginAtom() and
GetEndAtom(). These are needed to read data from a molecule in a const
function but also to modify atoms in a non-const function.

From an OB standpoint i believe there needs to be at least a decent
consideration to constifying some of the functions, at least the obvious
Get* functions.

From a Avo standpoint; what do we do about GetBeginAtom etc. The
obvious solution is to work around it with const_cast or to create two
different functions, one which returns a ‘const Atom*’ and one
that just returns ‘Atom*’. I haven’t thought thoroughly about this so
maybe there is a solution i just don’t know about. Any suggestions
welcome.

On Wed, 10 Jan 2007, Donald Ephraim Curtis wrote:

obvious solution is to work around it with const_cast

Nono, that’s a bad solution.

or to create two
different functions, one which returns a ‘const Atom*’ and one
that just returns ‘Atom*’

Yes, that’s how you do it. But then the method returning const must be
marked with the const modifier itself.

class SomeClass
{
protected:
VectorType m_vector;

public:
VectorType & GetVector()
{
return m_vector;
}

const VectorType & GetVector() const
{
	return m_vector;
}

};

Notice the second “const” at the end of the second prototype.

Benoit

I remove the openbabel-devel list from the ‘To’ field…

Just wanted to add that if you want to follow the Qt style (which you
might not want to, but just in case…), then you need to rename
GetVector() to vector(). IOW you remove the Get prefix from the simple
getters which return a (reference to a) member of the class. OTOH you
leave the Set prefixes.

VectorType & vector();
const VectorType & vector() const;
void SetVector( const VectorType & other );

(well, here SetVector is probably redundant. that’s just an example…)

Benoit

For libavogadro I am following that format. I hope that in the future
with OB (possibly 3.0) that there will be a huge refactoring of
functions. I am not sure how Geoff feels about this but i’m sure we’ve
talked about it before. I also prefer that functions start lowercase.

(Wed, Jan 10, 2007 at 09:15:36AM +0100) Benoit Jacob jacob@math.jussieu.fr:

I remove the openbabel-devel list from the ‘To’ field…

Just wanted to add that if you want to follow the Qt style (which you
might not want to, but just in case…), then you need to rename
GetVector() to vector(). IOW you remove the Get prefix from the simple
getters which return a (reference to a) member of the class. OTOH you
leave the Set prefixes.

VectorType & vector();
const VectorType & vector() const;
void SetVector( const VectorType & other );

(well, here SetVector is probably redundant. that’s just an example…)

Benoit

(Wed, Jan 10, 2007 at 09:06:49AM +0100) Benoit Jacob jacob@math.jussieu.fr:

Yes, that’s how you do it. But then the method returning const must be
marked with the const modifier itself.

class SomeClass
{
protected:
VectorType m_vector;

public:
VectorType & GetVector()
{
return m_vector;
}

const VectorType & GetVector() const
{
return m_vector;
}
};

Ugh. For some reason i was making both functions constant with one
returning a constant pointer and one not which makes me feel like a
moron for not realizing why that makes no sense. Thanks for clearing
this up. haha.