simple question on const function member passing

G

Giff

Hi,

I hope you can clear a doubt that is in my mind:

the function:

void food(const type* ptrToType);

means that it should not modify the object pointed by the pointer, or
merely the address of the pointer (and so the object might be
changed)?

Many thanks
 
S

SasQ

Dnia Sun, 01 Apr 2007 03:15:00 -0700, Giff napisa³(a):
void food(const type* ptrToType);

means that it should not modify the object pointed by the
pointer, or merely the address of the pointer (and so the
object might be changed)?

If you're in doubt, check where the const is. If it's near
the type, that means the object of that type pointed to is
const. If it's near the name of the pointer itself, it
means that pointer itself is constant and you can't point it
to another object.
 
G

Giff

If you're in doubt, check where the const is. If it's near
the type, that means the object of that type pointed to is
const. If it's near the name of the pointer itself, it
means that pointer itself is constant and you can't point it
to another object.


thanks, then it must be the pointed object :)
 
J

Jim Langston

Giff said:
Hi,

I hope you can clear a doubt that is in my mind:

the function:

void food(const type* ptrToType);

means that it should not modify the object pointed by the pointer, or
merely the address of the pointer (and so the object might be
changed)?

Many thanks

This can also be written as:
void food(type const* ptrToType);
Now read it right to left.

ptrToType is a pointer to a constant type.

If it was written this way:
void food(type* const ptrToType);
read it right to left again.

ptrToType is a constant pointer to type.

Using your original syntax works but a little more ackwardly.

void food(const type* ptrToType);

ptrToType is a pointer to type which is constant.
 
G

Giff

This can also be written as:
void food(type const* ptrToType);
Now read it right to left.

ptrToType is a pointer to a constant type.

Thank you, I'll try to use this form from now on.
 
J

Jim Langston

Giff said:
Thank you, I'll try to use this form from now on.

Most code I've seen uses:
const type* ptrToType

There is no hard and fast rule. As long as you understand what it's saying,
pick one way and stick with it. Neither way is wrong.
 
J

James Kanze

Most code I've seen uses:
const type* ptrToType

I've seen both.
There is no hard and fast rule. As long as you understand
what it's saying, pick one way and stick with it. Neither way
is wrong.

That's true up to a point. All other things being equal, the
post position is more coherent. All other things are never
equal however. Also: if your company or project has chosen one,
choosing the other *is* wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top