Solved

can someone please tell me what this does?

Posted on 2002-03-27
4
176 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
Comment Utility
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
Comment Utility
& 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
Comment Utility
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
Comment Utility
Correction: Another is not a type or a keyword, it is a reference to a variable of type "point".
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now