2

Pretty straightforward, I'm relearning C++ and I'm up to pointers. I understand the basic concept, but I'm having a little trouble. What would the syntax for calling this function be?

void getTime(int& hours, int& minutes, int& seconds) const
{
    hours = hr;
    minutes = min;
    seconds = sec;
}

The point of the function, as you've probably guessed, is to return the time in hours, minutes and seconds to three pointers in located in whatever scope the function call is in. Assume that hr, min, and sec have already been defined.

Also, if anyone would like to elaborate on the pointers a little (particularly when to use & and when to use *) that would be greatly appreciated. Thank in advance.

gmaster
  • 692
  • 1
  • 10
  • 27

4 Answers4

6

Your function signature uses &, which denotes a pass-by-reference. This means that you simply invoke your function as

int h, m, s;

// ... code ...

getTime(h, m, s);

Because your function passes these arguments by reference, whenever getTime() changes one of the arguments passed, the changes propagate to outside the function, as opposed to arguments passed by value where changes are limited to the function scope.

You can achieve a similar effect by passing pointers to variables instead:

void getTime(int * hours, int * minutes, int * seconds)
{
     // do stuff
}

// ... code ...

int h, m, s;
getTime(&h, &m, &s);

In the latter context, the & is the address-of operator. By dereferencing the addresses passed, your function can make changes to memory outside its function scope.

atomicinf
  • 3,596
  • 19
  • 17
4

The syntax for calling the function would be:

className.getTime(a, b, c);

Where className is the name of the class on which you defined this method and a, b, and c are all variables to integers. The ampersand & in the parameter list means the variables have the ability to have their values modified. Without it, you can modify the parameter but the corresponding argument will not be changed. There are no uses of pointers here, not from where I can see it.

Maybe you're confusing the unary & from the one in the parameter. What the unary & does is return the memory address of its operand. The address returned happens to be a pointer; however this has nothing to do with your example.

If you wanted to use pointers, you would have to utilize a function signature like this:

void getTime(int* a, int* b, int* c) const

* here means the function takes a pointer (the unary * is a dereference). We would call it like this:

int *a, *b, *c;

getTime(a, b, c);

Or like this:

int a, b, c;

getTime(&a, &b, &c);

The difference is that the variables in the first example are pointers, so there's no need to pass the address. In the second example, we need to pass the address because it yeilds a pointer to the object.

David G
  • 94,763
  • 41
  • 167
  • 253
1

What you have here are not pointers, but references. There are a few significant differences between the two. Firstly, references cannot be NULL, they must point to something that has been initialized. Secondly, the syntax required is difference.

As an example, if you were using pointers, the function would have the signature:

void getTime(int* hours, int* minutes, int* seconds) const
{
    hours = &hr;
    minutes = &min;
    seconds = &sec;
}

You'd then call it like so:

int hours, minutes, seconds;
hours = minutes = seconds = 0;
getTime(&hours, &minutes, &seconds);

References act in a similar way, that is, they are utilized to change the underlying argument that is passed in, as well as to avoid copying. The syntax is different, however:

int hours, minutes, seconds;
hours = minutes = seconds = 0;
getTime(hours, minutes, seconds); //Notice & not required

When would you utilize each? Generally, utilize references when you can, pointers when you must. In modern C++, pointers are often eschewed in favour of other constructs. They are still required for things such as polymorphism, or direct modification of values placed into containers (although this is most likely getting ahead of where you are, if none of this makes sense, just ignore it for now).

When passing a class or large object, utilize reference-to-const. So for example a std::vector is usually passed as void some_func(const std::vector<int>& v). If you really need to modify it, pass by reference, void some_func(std::vector<int>& v).

Yuushi
  • 25,132
  • 7
  • 63
  • 81
0

There are no pointers in your question, but there are references. This function would be called exactly the same as if it took normal arguments, that is:

//assuming an object called object that possesses the function:
//I am assuming its a object-function because of the 'const' modifier

int h, m, s;

object.getTime(h,m,s);

Here is a brief explanation of the difference between pointers and references:

Pointers store the memory address of another variable and are declared with the * character. Their value is an integer representing the location in computer memory of another variable; in order to access this value you must dereference operator (also the * character):

int * p; //p stores the location in memory of an int.
p = new int; //pointers must be dynamically allocated to avoid segfaults
p = &(some pre-existing int) //or set to the reference of another variable

*p = 7;//The * is used to access the data the pointer "points to"
delete p; //and must be deleted once they are done being used.

references are like normal variables in that they contain regular data but are distinct in that they maintain the same memory location when they are passed to functions (there may be more to this, I know that references do behave in this matter, although they may have some other definition):

int& i; //i is an int.
i = 7; //and can be assigned like one
//and cannot be dynamically allocated or deleted

Hope this helps.

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79