Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

killswitch

dot (.) operator vs ->

This topic is 6103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been working with c++ and mfc for a couple of weeks now, and I noticed that sometimes the books I have been reading use the . operator, and at times the -> operator as in: object.member object->member What functionally is the difference, and can the two be used interchangably?

Share this post


Link to post
Share on other sites
Advertisement
Hi!

The difference between these two operators is, that -> is used for pointers and . is used for normal variables.

E.g.

struct b
{
int nVar;
} *p, n;

you would use:

p->nVar and n.nVar;

Wildwest


Share this post


Link to post
Share on other sites
No they can''t be used interchangably. The left hand side of a "." must be an object as in

CMyObj obj;

obj.setx(5);

Whereas the left hand side of a "->" must be a pointer to an object, as in:

CMyObj obj, *objPtr;

objPtr = obj;

objPtr->setx(5);

which COULD be written as

(*objPtr).setx(5);

- seb

Share this post


Link to post
Share on other sites
I''m scared. Anyway.

. is different from ->

Consider:

typedef struct
{
int i;
}
MY_STRUCT;

MY_STRUCT s;
MY_STRUCT * p = &s;

s.i = 2;
(*p).i = 3;

Wouldn''t it be nice to shorten up the second assignment? Use the -> operator!

p->i = 4;

Hope this helps. Oh yeah, also get a book on C!

- Pete

Share this post


Link to post
Share on other sites
To complicate matters:

if the member is a function pointer, dereferencing that pointer calls the function.


struct mystruct {
void (*func)(void);
}

struct mystruct S;
S.func = foobar; // assign
S->func(); // call


// EDIT: To clarify long after the fact - this example uses straight C and the assignment should have been like this

S.func = &foobar; // assign
S->func(); // call


Edited by - lessbread on February 28, 2002 11:00:25 PM

Share this post


Link to post
Share on other sites
Just so nobody''s confused by it, LessBread was wrong. A pointer to a function is not called that way; there''s no difference between calling through a function pointer and a regular function name.

Correct example:

struct AStruct {
void (*FPtr)(); // Pointer to a void function taking no params
}

void VoidFunction()
{
}

...
AStruct str;
str.FPtr = VoidFunction;
str.FPtr(); // Calls VoidFunction


--
Eric

Share this post


Link to post
Share on other sites
To complicate a simple matter even further, I will add that you can overload the -> operator but not the . operator.
    
#include <StdIO.h>

struct Object
{
void Function ( )
{
printf( "Object::Function()\n" );
}
};

struct Interface
{
inline Object* operator -> ( )
{
return &var;
}

void Function ( )
{
printf( "Interface::Function()\n" );
}

Object var;
};

void main ( )
{
Interface I;

I.Function( );
I->Function( );
}


~CGameProgrammer( );



Edited by - CGameProgrammer on February 4, 2002 12:58:11 AM

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
if the member is a function pointer, dereferencing that pointer calls the function.



This is incorrect. Dereferencing a function pointer does not result in a call to the function. That''s what the function call operator "()" does.

--
You see some pale bulbous eyes staring at you.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!