Solved

can someone please tell me what this does?

Posted on 2002-03-27
4
199 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

809 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