Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

can someone please tell me what this does?

Posted on 2002-03-27
4
Medium Priority
?
222 Views
Last Modified: 2010-04-02
I'm taking a beginner's C++ class, and long story, but for now I have to work without the textbook.

This is from a class point (for a triangle) example.

I have a basic understanding of classes, but what I can't find any help on is this:

double distance (const point& another) const;

My questions: what does the ampersand at the end of point& do, and is "another" a keyword, or is it an arbitrary assignment, and what does it do?

Thank You Thank You, for your time.
0
Comment
Question by:celere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6901830
The ampersand means the variable takes a reference, and "another" is a type, not a keyword.
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 200 total points
ID: 6901833
& means "reference" in C++. As in your example, the code says that you pass the argument into the function as reference, which means that the argument you passed into the function can be changed and the change inside the function is reflected outside the function.

Let me give another example to illustrate and help you to understand:

void fn(int& x) {
   x=1;
}

void fn1(int x) {
   x=4;
}

int main() {
  int x=9;
  cout << "Before: " << x << endl;
  fn(x);
  cout << "After fn: " << x << endl;
  fn1(x);
  cout << "After fn1: " << x << endl;
  return 0;
}

In this example, the output is:
Before: 9
After fn: 1
After fn1: 1

=========================================================
"another" is not a keyword, it is just a name of the argument. You can replace it with any valid name you like (of course you need to change the name of all the instances with the name "another" inside that function as well).
0
 
LVL 3

Expert Comment

by:MDarling
ID: 6902029
Another is not a type or a keyword, it is a variable of type "point".

I'm guessing that

double distance (const point& another) const;

is supposed to be a member function of the class point.  If so then good old Pythagoras would help you determine the distance from point to point.

class point
{
double x,y;
public:
point(double _x,double _y);
double distance(const point& another) const
{
   double d=...;
   // pythagoras
}
};

Regards,
Mike.
0
 
LVL 3

Expert Comment

by:MDarling
ID: 6902031
Correction: Another is not a type or a keyword, it is a reference to a variable of type "point".
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question