[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

can someone please tell me what this does?

Posted on 2002-03-27
4
Medium Priority
?
224 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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 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…
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.
Suggested Courses

872 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