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.

Dani AI

Generated

Good concise answer from — a few practical points for someone moving from Java/C#.

C++ distinguishes raw pointers, references and actual objects. The arrow form is the syntax you use when you have a pointer-like thing; dot is used when you have an object or a reference. Because ownership and lifetime are explicit in C++, watch for null or dangling pointers: using -> on a null/dangling pointer is undefined behavior, so prefer references when a null state is not meaningful, and prefer smart pointers for ownership management. See std::unique_ptr for a modern alternative to raw owning pointers: std::unique_ptr (cppreference).

Also note that operator-> can be overloaded. That is how smart-pointer wrappers let you write ptr->member even though ptr is a class type; the compiler will call operator->() (and will keep applying it if the returned type itself has operator->) until it reaches a raw pointer. For details on the language rule and examples, see operator-> (cppreference).

Quick examples and tips:

  • Many standard and third-party smart pointers implement operator->, so sp->foo() is idiomatic.
  • If expressions combine . and ->, remember grouping rules: parenthesize complex expressions to make intent clear.
  • When you run into pointer-to-pointer cases, you usually need one explicit dereference before using ->; using references instead avoids that complexity.

For searching, include the words "C++ operator->" or "C++ arrow operator" — punctuation alone often gets ignored by general search engines, so add keywords or go straight to cppreference or a language reference.

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.

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

Google Code is able to do it, however Google itself, ignores punctation, as a rule of thumb!

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.