Difference Between '->' and '.' Operators

Difference Between '->' and '.' Operators

Post by David Klemit » Tue, 14 Aug 2001 21:44:10



Can someone point me in the direction (if you'll excuse the pun) of a
good explanation of the difference between '->' and '.'.

As far as I understand '->' is used when accessing data or methods in a
class when the class is defined on the heap. '.' is used when accessing
data or member methods on the stack.Does this apply to struct as well as
classes. How do you know you are using the stack or heap anyway. This
and any other information that would help understand the difference
would be helpful

thanks

David Klemitz

 
 
 

Difference Between '->' and '.' Operators

Post by Attila Fehe » Tue, 14 Aug 2001 21:59:05



> Can someone point me in the direction (if you'll excuse the pun) of a
> good explanation of the difference between '->' and '.'.

> As far as I understand '->' is used when accessing data or methods in a
> class when the class is defined on the heap. '.' is used when accessing
> data or member methods on the stack.Does this apply to struct as well as
> classes. How do you know you are using the stack or heap anyway. This
> and any other information that would help understand the difference
> would be helpful

Nope.  Not at all.  -> is used when you access class/struct via a
pointer, while . is used when it isn't one:

struct Foo {
   int bar;

Quote:};

//...

Foo x;
Foo *y;

y=&x;

cout << "This is x.bar: " << x.bar << endl;

cout << "This is also x.bar,"
" but via y: " << y->bar << endl;

cout << "And the last one: " << (*y).bar << endl;

Here x is an object (or could be a reference to it).  The y is a pointer
to an object (instance of a struct in this case).  So *y is the objects,
so (*y).bar is the same as y->bar.

Attila

 
 
 

Difference Between '->' and '.' Operators

Post by Jeff Connell » Wed, 15 Aug 2001 00:19:00



Quote:> Can someone point me in the direction (if you'll excuse the pun) of a
> good explanation of the difference between '->' and '.'.

> As far as I understand '->' is used when accessing data or methods in a
> class when the class is defined on the heap. '.' is used when accessing
> data or member methods on the stack.

Not necessarily, but basically.  -> is pointer notation.

Quote:>Does this apply to struct as well as classes.

Yes, of course.  They are the same thing.

Quote:> How do you know you are using the stack or heap anyway.

class A
{
public:
 void f() {}

Quote:};

A* pA = new A;  // heap, dynamic
A a;  // stack, static

pA->f();
a.f();

 
 
 

Difference Between '->' and '.' Operators

Post by Jeff Connell » Wed, 15 Aug 2001 00:21:16




> > Can someone point me in the direction (if you'll excuse the pun) of a
> > good explanation of the difference between '->' and '.'.

> > As far as I understand '->' is used when accessing data or methods in a
> > class when the class is defined on the heap. '.' is used when accessing
> > data or member methods on the stack.Does this apply to struct as well as
> > classes. How do you know you are using the stack or heap anyway. This
> > and any other information that would help understand the difference
> > would be helpful

> Nope.  Not at all.  -> is used when you access class/struct via a
> pointer, while . is used when it isn't one:

"Not at all"??  How is storage for pointers usually allocated?  How is
storage for non-pointer variables usually allocated?  I think the OP was on
the right track - just needed some clarification.
 
 
 

Difference Between '->' and '.' Operators

Post by Ron Natali » Wed, 15 Aug 2001 01:05:50



> > Nope.  Not at all.  -> is used when you access class/struct via a
> > pointer, while . is used when it isn't one:

> "Not at all"??  How is storage for pointers usually allocated?  How is
> storage for non-pointer variables usually allocated?  I think the OP was on
> the right track - just needed some clarification.

It's got nothing to do with how things are allocated.  If the operand to
the left is a pointer you must apply -> to it.  You can only apply . to
something that is an object.   How the pointer or object came into being
or where it is allocated is immaterial to the decision to apply -> or . to
it.

The only other difference is that -> may be overloaded (in this case, the
thing to the left is an object rather than a pointer).

 
 
 

Difference Between '->' and '.' Operators

Post by Attila Fehe » Wed, 15 Aug 2001 01:03:49






> > > Can someone point me in the direction (if you'll excuse the pun) of a
> > > good explanation of the difference between '->' and '.'.

> > > As far as I understand '->' is used when accessing data or methods in a
> > > class when the class is defined on the heap. '.' is used when accessing
> > > data or member methods on the stack.Does this apply to struct as well as
> > > classes. How do you know you are using the stack or heap anyway. This
> > > and any other information that would help understand the difference
> > > would be helpful

> > Nope.  Not at all.  -> is used when you access class/struct via a
> > pointer, while . is used when it isn't one:

> "Not at all"??  How is storage for pointers usually allocated?

Don't make me mad!  They have _nothing_ to do with each other!  In C
interfaces the structures are (and can only be) passed as pointers!  Are
they allocated on the stack?  Depending on your program it may well be
true that most of them are _not_.  There are _several_ (like
gethostbyname) functions returning pointer to a static structure...  Are
those allocated on the stack?  NO!

Quote:>  How is storage for non-pointer variables usually allocated?

ANY way I want:

long void drinkVodka( const VodkaDesc &vd, long long dl) {
 //... glup

Quote:}

// VodkaDesc is abstract

int main(void) {
  VodkaDesc *avd;

  avd = abstractVodkaFactory( cin, dontPayTaxes /*enum*/);

  drinkVodka( *avd, 150);
  delete avd;
  abort();

Quote:}
>  I think the OP was on the right track - just needed some clarification.

And I think not.  The _way_ teh structures/classes are _allocated_ has
_nothing_ to do with the way we access them.

Attila

 
 
 

Difference Between '->' and '.' Operators

Post by Mike Smit » Wed, 15 Aug 2001 01:59:07




> > "Not at all"??  How is storage for pointers usually allocated?

> Don't make me mad!  They have _nothing_ to do with each other!

Dude.  I believe the old saw about the availability of various brands of
decaf that taste almost as good as the real thing would be appropriate here.

--
Mike Smith

 
 
 

Difference Between '->' and '.' Operators

Post by Clark S. Cox I » Wed, 15 Aug 2001 03:04:45






> > > Can someone point me in the direction (if you'll excuse the pun) of a
> > > good explanation of the difference between '->' and '.'.

> > > As far as I understand '->' is used when accessing data or methods in a
> > > class when the class is defined on the heap. '.' is used when accessing
> > > data or member methods on the stack.Does this apply to struct as well as
> > > classes. How do you know you are using the stack or heap anyway. This
> > > and any other information that would help understand the difference
> > > would be helpful

> > Nope.  Not at all.  -> is used when you access class/struct via a
> > pointer, while . is used when it isn't one:

> "Not at all"??  How is storage for pointers usually allocated?

    You're saying that pointers can't point to statically allocated
objects?

struct Foo  {
    int bar;

Quote:}

int main()
{
    Foo f;
    Foo *fp = &f;

    //No "heap" involved
    fp->bar = 0;

    return 0;

Quote:}
> How is storage for non-pointer variables usually allocated?

    You're saying that you don't count dereferenced pointers as
non-pointers?

struct Foo  {
    int bar;

Quote:}

int main()
{
    Foo *fp = new Foo;

    {
        Foo &f  = *fp;
        fp.bar  = 0;
    }

    delete fp;

    return 0;

Quote:}
> I think the OP was on the right track - just needed some clarification.

    Yes, he seemed to be on the right track, but needed a little more
than clarification.

--
Clark S. Cox III

http://www.whereismyhead.com/clark/

 
 
 

Difference Between '->' and '.' Operators

Post by Lars Juel Jense » Wed, 15 Aug 2001 05:24:12


Hihihi....

This is funny.. Here you guys are using advanced stuff to someone who asks
about . and ->.
Jeff - I agree with you. The USUAL storage of a pointer is on the heap. You
ALLOCATE
memory on the heap. Just like this

CObject * pObject = new CObject();

Now, the USUAL storage of an object is on the stack. You DO NOT ALLOCATE
memory
on the stack. The compiler sees to that it get's there. Like this

CObject object;

This is the USUAL case, but there are of course other ways of doing things.
Like Clark suggests,
you can do this

CObject object;
CObject * pObject = &object;

Now this will make the pointer point to the STACK, where the compiler has
placed the memory of
the CObject instance named object.

I hope this clearified some things. Or you guys - please help me out here.
Where am I wrong?

LJJ

 
 
 

Difference Between '->' and '.' Operators

Post by Attila Fehe » Wed, 15 Aug 2001 15:03:53






> > > "Not at all"??  How is storage for pointers usually allocated?

> > Don't make me mad!  They have _nothing_ to do with each other!

> Dude.  I believe the old saw about the availability of various brands of
> decaf that taste almost as good as the real thing would be appropriate here.

I don't drink cofee.

A

 
 
 

Difference Between '->' and '.' Operators

Post by Pat Butche » Wed, 15 Aug 2001 17:17:09



> Hihihi....

Hi.

Quote:> This is funny.. Here you guys are using advanced stuff to someone who asks
> about . and ->.

[snip]

Quote:

> I hope this clearified some things. Or you guys - please help me out here.
> Where am I wrong?

There's nothing wrong with what you've said - it's just
that the storage type of the thing that you're pointing
to is immaterial wrt the distinction between '.' and '->'.

Often cut-down examples seem a little bit odd. Why would
you do:

Obj o;
Obj *p = &o;

But it's not uncommon to do:

void f(Obj *p);

Some people regard this as good style - ie use const references
to pass complex types unless you are going to modify the object
inside the method/function. Thus it doesn't matter where the
object resides in memory.

It's okay to think about the USUAL way of doing things but you
do need to know about all of the different ways that things may
be done (especially if/when you have to work with other people's
code!).

Pat

 
 
 

Difference Between '->' and '.' Operators

Post by Stuart Golodet » Sat, 18 Aug 2001 02:46:27





> > Can someone point me in the direction (if you'll excuse the pun) of a
> > good explanation of the difference between '->' and '.'.

> > As far as I understand '->' is used when accessing data or methods in a
> > class when the class is defined on the heap. '.' is used when accessing
> > data or member methods on the stack.

> Not necessarily, but basically.  -> is pointer notation.

Ignoring overloading for a minute:

a->b is the same as (*a).b, it's a shorthand form effectively. Of course,
because you can overload operator->(), this has complications, but that's
the basic point to understand...

Quote:

> >Does this apply to struct as well as classes.

> Yes, of course.  They are the same thing.

With the additional point that the default access specifier for a class is
private (also default inheritance is private) and the default access
specifier for a struct is public (and default inheritance is public), other
than that they are identical as Jeff says... :-)

HTH,

Stuart.

Quote:

> > How do you know you are using the stack or heap anyway.

> class A
> {
> public:
>  void f() {}
> };

> A* pA = new A;  // heap, dynamic
> A a;  // stack, static

> pA->f();
> a.f();

 
 
 

1. Any reason C++ couldn't adopt Java's '>>>' operator?

Reading up on Java I came across the zero fill right shift operator
'>>>'.  This would be quite handy for the types of software I write.
Can any one see a reason why this operator could not be added to the C++
spec?  Doesn't seem like it would break anything.

Speaking of break... Java's "break label" and "continue label" features
are also quite handy.  Seems like these could also be incorporated
without breaking any old code.  Break without a label could just revert
to quiting the current block.

Just thinking....
--
Craig Johnston                                  Boeing Defense & Space Group


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. xstat logging

3. 'new' operator and 'operator new' - confusing me

4. NSA cdrom is 100% compatable with one other... What Is IT????

5. TreeView: what's the difference btw 'child' and 'sibling'?

6. looking for old print shop titles

7. What's the difference between '&' and '+'

8. FS USED TERMINALS: Wyse, IBM, Decision Data, Digital and more...

9. differences bet OS/2 'C' and win32 'C' language

10. difference between 'id' and 'Object'

11. "error C2582: 'SubClass' : 'operator =' function is unavailable"

12. Overloading the 'new' and 'delete' operators

13. Temp object constructed in 'if' expression with no '=' (assign) operator correctness.