473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

difference between *foo and (*foo) ?

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
Jan 6 '06 #1
6 2963

Manuel wrote:
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.

Jan 6 '06 #2
On 2006-01-06, Manuel <ma**********************@tin.it> wrote:
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
Jan 6 '06 #3
Manuel wrote:
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
Jan 6 '06 #4
Russell Wood wrote:
s2->setBackgroundColor(...);


ok.

So

s2->setBackgroundColor(...);

is perfectly equal to

(*s2).setBackgroundColor(...);
?
Jan 6 '06 #5
Manuel wrote:
Russell Wood wrote:
s2->setBackgroundColor(...);

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
Jan 6 '06 #6
On 2006-01-06, Manuel <ma**********************@tin.it> wrote:
Russell Wood wrote:
s2->setBackgroundColor(...);


ok.

So

s2->setBackgroundColor(...);

is perfectly equal to

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


Correct.
Jan 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
7
by: Paul | last post by:
Say I have: char foo; char** bar; In my understanding, these two have the same meaning in memory. Could someone elucidate upon these two types and their meanings? Thanks, Paul
5
by: Gunnar G | last post by:
What is the difference between the two lines in the main function? class MyClass{ ....}; int main(){ MyClass a(); MyClass a; }
10
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
5
by: sophie_newbie | last post by:
OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? And if i do...
4
by: Sugan | last post by:
Hi, i came across custom usercontrols in VB 2005 and i'm not clear about the difference between "Me" and "MyBase". 1. What is the difference between the two keywords in a UserControl. 2....
6
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they...
5
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.