5

I am new to C++ and I am trying to understand some code. What does it mean to have a * in front of the datatype ? and why is the class Name in front of the method name CAStar::LinkChild

void CAStar::LinkChild(_asNode *node, _asNode *temp) { } 

3 Answers 3

6
  1. A * in front of the data type says that the variable is a pointer to the data type, in this case, a pointer to a node. Instead of passing a copy of the entire "node" into the method, a memory address, or pointer, is passed in instead. For details, see Pointers in this C++ Tutorial.

  2. The class name in front of the method name specifies that this is defining a method of the CAStar class. For details, see the Tutorial pages for Classes.

Sign up to request clarification or add additional context in comments.

1 Comment

Furthermore, you can think of it as a way of disambiguating function names. For example, say you have two classes (trivial example); one implements fractions (Fraction) and the other implements integers (Integer). Both classes may have a function called "add". By including the class name Fraction, the compiler will know that you are implementing the add function for the Fraction class, rather than the Integer class.
3

The * means that it's a pointer. You will also find that _asNode *node is equivalent to _asNode* node.

The class name is in front of the method name when the method is not defined inside the class { ... }. The :: is the scope operator.

Comments

2

Are you new to programming in general, or just to C++? If you're new to programming, you probably want to take some classes. If you're just new to C++, you could try reading Practical C++ Programming an on-line C++ Primer.

Regarding your specific question: in the variable declaration, an asterisk means "this is a pointer":

 int * pointer; 

This also covers function declarations/prototypes, where the variables are declared,as in your example.

After the declaration, asterisk means that you're de-referencing the pointer. That is, you're getting the value at the location to which it's pointing.

 printf("memory address:%d value:%d", pointer, *pointer); 

You'll note that memory address will change unexpectedly, depending upon the state of the program when it's printed. In a simple program, you won't see the change, but in a complex program, you would.

1 Comment

I am new to C++. I've done work in mostly php, as3, and C#. but never in c++. But hearing these responses I am seeing that C++ is lil different. Like declaring methods outside of a class. But thanks for the info.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.