difference between *foo and (*foo) ?

M

Manuel

Hi,

I've read that a line like this:

square *s1 = new square(0,0,20,20);

- alloc the memory needed for obj square,
- create a new object ,
- and return the address into the pointer s1

so, if I use the dereference operator, I access the obj.
The square obj has a method called "setBackgroundColor", so I thinked
to use simply:

*s2.setBackgroundColor(0.0,0.0,0.8,0.5);

but this not work:
- request for member of non-aggregate type before '(' token

Instead, if I use:

(*s2).setBackgroundColor(0.0,0.0,0.8,0.5);

it work.

I think that the first don't work because the compiler consider

*s2.setBackgroundColor(0.0,0.0,0.8,0.5);
as
*(s2.setBackgroundColor(0.0,0.0,0.8,0.5));

but it's only a my idea, so I'm looking for a "real" explanation :)

thx,

Manuel
 
N

Neelesh Bodas

Manuel said:
I think that the first don't work because the compiler consider

*s2.setBackgroundColor(0.0,0.0,0.8,0.5);
as
*(s2.setBackgroundColor(0.0,0.0,0.8,0.5));

but it's only a my idea, so I'm looking for a "real" explanation :)

The member selection operator (.) has a higher precedence than the
dereference operator.
 
R

Russell Wood

Hi,

I've read that a line like this:

square *s1 = new square(0,0,20,20);
...
so, if I use the dereference operator, I access the obj.
The square obj has a method called "setBackgroundColor", so I thinked
to use simply:

*s2.setBackgroundColor(0.0,0.0,0.8,0.5);

but this not work:
- request for member of non-aggregate type before '(' token

Instead, if I use:

(*s2).setBackgroundColor(0.0,0.0,0.8,0.5);

it work.

It's because your object is a pointer:

s2->setBackgroundColor(...);

- Russell
 
V

Victor Bazarov

Manuel said:
I've read that a line like this:

square *s1 = new square(0,0,20,20);

- alloc the memory needed for obj square,
- create a new object ,
- and return the address into the pointer s1

Rather, "- and initialise the pointer 's1' with that address".
so, if I use the dereference operator, I access the obj.
Yes.

The square obj has a method called "setBackgroundColor", so I thinked
to use simply:

*s2.setBackgroundColor(0.0,0.0,0.8,0.5);

but this not work:
- request for member of non-aggregate type before '(' token

Instead, if I use:

(*s2).setBackgroundColor(0.0,0.0,0.8,0.5);

it work.

"Dot" operator has precedence over the "asterisk" operator.
I think that the first don't work because the compiler consider

*s2.setBackgroundColor(0.0,0.0,0.8,0.5);
as
*(s2.setBackgroundColor(0.0,0.0,0.8,0.5));

but it's only a my idea, so I'm looking for a "real" explanation :)

Find a decent book where operators and their precedence are explained.

V
 
V

Victor Bazarov

Manuel said:
ok.

So

s2->setBackgroundColor(...);

is perfectly equal to

(*s2).setBackgroundColor(...);


?

In your case, yes. IOW, if 's2' is a pointer, then they are the same.

If 's2' is an object, you can have operator-> overloaded for it and
operator* overloaded, in such a way that those two expressions do not
produce the same result. But I am guessing that you're not at that
stage yet.

V
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top