Hi everyone, I just can't seem to find an explanation for using this "->" in code, in C++. I've migrated from Java and C#, and haven't seen this notation before. Basic example:

CMesh *pMesh = WorldObjects[i]->m_pMesh;

As best as I can tell, this is the C++ equivalent of C#'s

WorldObjects[i].m_pMesh;

. (as if to say that m_pMesh is a member variable, or method of the WorldObjects object, but as it is only a code snippet, I cant check the classes to see)

Just a quick syntactic explanation is all I need. Thanks.

Recommended Answers

All 4 Replies

For a class or a struct that's being referred to by a pointer, -> represents the dereference of the pointer followed by a dot operator.

For example say you have a class MyClass and public member function myMethod:

MyClass * mycl = new MyClass();
mycl->myMethod();
would be the same as
(*mycl).myMethod();

For a class or a struct that's being referred to by a pointer, -> represents the dereference of the pointer followed by a dot operator.

For example say you have a class MyClass and public member function myMethod:

MyClass * mycl = new MyClass();
mycl->myMethod();
would be the same as
(*mycl).myMethod();

Thanks so much, that takes ALL the confusion out of it. The worst part was trying to google "->" - Google didnt seem to recognise that as a search term, lol.

Thanks!

The worst part was trying to google "->"

Yeah, I've been through that myself. I've never found a way around it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.