pointers

Greetings,

I haven't seen such a statement in c++ so far. Is G4Track a pointer?

void ScoreNewTrack(const G4Track*);

Thnak you
G4Track is a type.
But const G4Track* is a pointer to a const G4Track.

So you will use this function like this:
1
2
3
4
5
6
7
G4Track myG4Track(/*provide the necessary arguments to create a G4Track*/);
ScoreNewTrack(& myG4Track); // & x yields the address of the variable x, i.e. create a pointer to x

// or

G4Track* myG4TrackPointer = new G4Track(/*provide the necessary arguments to create a G4Track*/);
ScoreNewTrack(myG4TrackPointer);
So if it is a pointer, is there any difference of the two writings below?

void ScoreNewTrack(const *G4Track);

and

void ScoreNewTrack(const G4Track*);


The first one is incorrect syntax. It is meaningless. It states that the function accepts an object named G4Track, of type const *, which is meaningless. You cannot have just a const pointer, it must be a const pointer to a type.

The second one states that the function accepts a pointer to a const G4Track, which makes a lot of sense.
Last edited on
What you can switch (to some extend is the const):
1
2
3
4
5
void ScoreNewTrack(const G4Track*);
// is the same as
void ScoreNewTrack(G4Track const *);
// but NOT the same as
void ScoreNewTrack(G4Track * const);


Some prefer the second version because you can read it "from right to left":
Pointer to a const G4Track.
The third version is different since if you read it "right to left" again:
A const pointer to a G4Track.
Here the pointer is const and not the pointed to G4Track.

So can I say that the pointer has a type but not a name?

The type is a const G4Track but there is no name for the pointer!
That's correct. You do not need to name the parameter in the function prototype; the compiler has no use for them in the prototype. The actual function definition needs to name the parameters so they can be used in the function body.
Topic archived. No new replies allowed.