Solved

can someone please tell me what this does?

Posted on 2002-03-27
4
193 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
  • 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 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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