Reference / Pointer

Here is a design question;

Say we have the following:

class X {
private:
AnyType x;
}

If we want to have a function say X::getX() should we have it return by
reference or return a pointer? I believe it should be a return by
pointer but i want to be clear about it.

Also, when should a function have a parameter passed by reference and
when should the parameter be a pointer. I don’t care for pass by
reference all that much but this made me think when talking about the
Engine::render(PrimitiveQueue *q) function whether i should actually
pass q by reference. Like i said, i don’t like reference but i didn’t
read the C++ FAQ lite cover to cover so maybe i missed something about
it.

Guess i just thought i’d ask before i start passing all by pointer.

-Donald

Hi

It seems to me that getX() should return a reference to x, not a pointer.

As far as I know, the rule of thumb is : use pointers when you’re not
sure that the object you want to point to exists. Pointers are great for
that because when the object does not exist you can return the 0 pointer.
But in your case you know that x exists, so why not return a reference?

For the passing of arguments, i didn’t look at your code so I can’t be
sure, and also I must say that it’s often not 100% clear to me whether one
should use pointers or references. I’d say:

  1. try to use the rule of thumb above
  2. try choosing the approach that suits best your code: if in your code
    the object you want to pass as argument is already handled by a pointer,
    it makes sense to pass it as a pointer (no syntax overhead).

Benoit

On Wed, 13 Sep 2006, Donald Ephraim Curtis wrote:

Here is a design question;

Say we have the following:

class X {
private:
AnyType x;
}

If we want to have a function say X::getX() should we have it return by
reference or return a pointer? I believe it should be a return by
pointer but i want to be clear about it.

Also, when should a function have a parameter passed by reference and
when should the parameter be a pointer. I don’t care for pass by
reference all that much but this made me think when talking about the
Engine::render(PrimitiveQueue *q) function whether i should actually
pass q by reference. Like i said, i don’t like reference but i didn’t
read the C++ FAQ lite cover to cover so maybe i missed something about
it.

Guess i just thought i’d ask before i start passing all by pointer.

-Donald


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


Avogadro-devel mailing list
Avogadro-devel@lists.sourceforge.net
avogadro-devel List Signup and Options

Well, it depends. Because you want to be consistent. If you do getX
and you don’t return a pointer; a developer should assume they are
recieving a copy, not a reference.

I guess naming it getX was more like X::getFoo().

Now that i think about it; return by reference doesn’t seem like a great
idea at all.

Thanks for the response; it made me think about it a different way.

-Donald

(Thu, Sep 14, 2006 at 07:08:59AM +0200) Benoit Jacob jacob@math.jussieu.fr:

Hi

It seems to me that getX() should return a reference to x, not a pointer.

As far as I know, the rule of thumb is : use pointers when you’re not
sure that the object you want to point to exists. Pointers are great for
that because when the object does not exist you can return the 0 pointer.
But in your case you know that x exists, so why not return a reference?

For the passing of arguments, i didn’t look at your code so I can’t be
sure, and also I must say that it’s often not 100% clear to me whether one
should use pointers or references. I’d say:

  1. try to use the rule of thumb above
  2. try choosing the approach that suits best your code: if in your code
    the object you want to pass as argument is already handled by a pointer,
    it makes sense to pass it as a pointer (no syntax overhead).

Benoit

On Wed, 13 Sep 2006, Donald Ephraim Curtis wrote:

Here is a design question;

Say we have the following:

class X {
private:
AnyType x;
}

If we want to have a function say X::getX() should we have it return by
reference or return a pointer? I believe it should be a return by
pointer but i want to be clear about it.

Also, when should a function have a parameter passed by reference and
when should the parameter be a pointer. I don’t care for pass by
reference all that much but this made me think when talking about the
Engine::render(PrimitiveQueue *q) function whether i should actually
pass q by reference. Like i said, i don’t like reference but i didn’t
read the C++ FAQ lite cover to cover so maybe i missed something about
it.

Guess i just thought i’d ask before i start passing all by pointer.

-Donald


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


Avogadro-devel mailing list
Avogadro-devel@lists.sourceforge.net
avogadro-devel List Signup and Options

For such simple functions; i prefer the following
class X
{
public:
AnyType& x() {return x};
const AnyType& x(){ return x; }
}

Because in fact is only syntactic sugar
However things can be a more formal than thought due to compiler optimizations

Normally the recommend syntax for functions line render(Primitive Queue* q) is
pass a reference for consts objects and pointer for objects which are to be
modified

Here is a design question;

Say we have the following:

class X {
private:
AnyType x;
}

If we want to have a function say X::getX() should we have it return by
reference or return a pointer? I believe it should be a return by
pointer but i want to be clear about it.

Also, when should a function have a parameter passed by reference and
when should the parameter be a pointer. I don’t care for pass by
reference all that much but this made me think when talking about the
Engine::render(PrimitiveQueue *q) function whether i should actually
pass q by reference. Like i said, i don’t like reference but i didn’t
read the C++ FAQ lite cover to cover so maybe i missed something about
it.

Guess i just thought i’d ask before i start passing all by pointer.

-Donald


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


Avogadro-devel mailing list
Avogadro-devel@lists.sourceforge.net
avogadro-devel List Signup and Options


Dr. Armando Navarro Vázquez
RIAIDT. Universidade de Santiago de Compostela