dot (.) operator vs ->
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?
-> (arrow) is used when using pointers, the membership (.) is used when you don''t use pointers. Simple, eh?
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
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
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
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
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
. 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
To complicate matters:
if the member is a function pointer, dereferencing that pointer calls the function.
// EDIT: To clarify long after the fact - this example uses straight C and the assignment should have been like this
Edited by - lessbread on February 28, 2002 11:00:25 PM
if the member is a function pointer, dereferencing that pointer calls the function.
struct mystruct { void (*func)(void);}struct mystruct S;S.func = foobar; // assignS->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 // assignS->func(); // call
Edited by - lessbread on February 28, 2002 11:00:25 PM
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
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
To complicate a simple matter even further, I will add that you can overload the -> operator but not the . operator.
~CGameProgrammer( );
#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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement