pointers

Started by
14 comments, last by FGFS 10 years, 8 months ago

Hi

would:

virtual void appendWaypoint(const Waypoint& wpt);

be different to:

virtual void appendWaypoint(const Waypoint &wpt);

or to?

virtual void appendWaypoint(const &Waypoint wpt);

Well I don't understand any.

Thanks

Advertisement

The first two are the same. The third won't compile. Also, these aren't pointers; the & syntax is for references.

Si is right. The first two are the same thing. They are a matter of personal programming style. The second one should not compile. Thirdly, these technically are not pointers, they are references. Pointers are declared with the "*" symbol and work a little differently than references.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

What you really should write is virtual void appendWaypoint(Waypoint const& wpt); because then you will not run into problems with certain template and lambda syntaxes. You would read that parameter declaration aloud as "the Waypoint const reference wpt", same as if it were spelled const Waypoint &wpt except all the words are in the same order.

The confusion over the '&' token indicating a pointer is because it is overloaded as the unary address-of operator, used to get the address of a variable (generate a pointer to the variable). A pointer is declared using the '*' token, which is overloaded as the dereferenced-value-of operator, used to get the value addressed by a pointer. In summary:


int an_int = 7;                            // an_int is of type int containing the value 7.
int& reference_to_int = an_int;            // reference_to_int is an alias for an_int.
int* pointer_to_int = &reference_to_int;   // the dereferenced value of pointer_to_int is of type int,
                                           // and pointer_to_int is a pointer initialized to the address of an_int.
int another_int = *pointer_to_int;         // another_int will now have the value 7.

Stephen M. Webb
Professional Free Software Developer

Si is right. ... The second one should not compile. ...

You mean the third one, right?

Here's an explanation from Bjarne Stroustrup himself (about pointers, not references, but it also boils down to whitespace, just like it does for the first two cases you're asking about): http://www.stroustrup.com/bs_faq2.html#whitespace

Still a little confused. Are those the same?

typedef vector<Waypoint*> StdWaypointList;

typedef vector<*Waypoint> StdWaypointList;

Thanks

First one is correct, second one is illegal syntax.

typedef vector<Waypoint*> StdWaypointList;

typedef vector<Waypoint> *StdWaypointList;

would that be the same? Thanks

No, they are fundamentally different. One is a pointer to a vector of Waypoints, the other is a vector of pointers to Waypoints.

This topic is closed to new replies.

Advertisement