Archived

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

Newbie Questions

This topic is 5309 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

We use "." to call a function in a Class.(e.g.: Class.GetValue) Sometime,I saw some sorce code use "->" especially DirectX stuff [e.g.ddsdSurface->Blt(NULL,BMP,NULL,DDBLT_WAIT,NULL)] Why''s that? Can someone give me a brief explain? Thx for help.I''m really a newbie.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You use "->" when the object is a pointer.

CClass * pobject_a;

pobject_a = new CClass;

pobject_a->GetValue();

Share this post


Link to post
Share on other sites
The ''.'' operator is for an instance of an object. The ''->'' operator is for a pointer to an object. For example:



typdef struct{
int a;
int b;
} mystruct;


main(){
mystruct str1, *str2;

str2 = new mystruct;

//to set the values in str1, use ''.''

str1.a = 5;

//to set the values in str2, use ''->''

str2->a = 10;

//Don''t mix and match


str1->b = 8; // This will either not compile or seg fault at runtime


Share this post


Link to post
Share on other sites
Imagine you have a pointer to a class:

CSomeClass* pClassInstance;

To access a member of pClassInstance you must first dereference the pointer by doing this:

(*pClassInstance).SomeMethod();

Because this is tedious -> was invented so you could type this instead:

pClassInstance->SomeMethod();

Hope that helps...

Share this post


Link to post
Share on other sites