-1

I don't understand the 2nd argument. Whats is it exactly? And most importantly he(the programmer) uses it to create a new array of Object objects , of num (the variable) size at the end.

void expand(const Object &s, Object* &children, int &num)
{
   ...
   children = new Object[num]; // <----
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
DIMITRIOS
  • 305
  • 1
  • 3
  • 10
  • I would suggest you read a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It is, obviously, a reference to a pointer to `Object`. What's unclear about that? – Algirdas Preidžius Mar 17 '17 at 20:30
  • It is ugly, but you can use the [Clockwise/Spiral Rule](http://c-faq.com/decl/spiral.anderson.html) to decipher most C and C++ declarations. – Rama Mar 17 '17 at 20:33
  • Algirdas Preidžius: So the function takes as a 2nd argument the MEMORY ADDRESS of the pointer named children , which points to type Object? – DIMITRIOS Mar 17 '17 at 20:39
  • @DIMITRIOS That's why I suggested reading a C++ book. Reference != memory address. Probably you are confusing references, and address-of operator? – Algirdas Preidžius Mar 17 '17 at 20:40
  • See [this answer](http://stackoverflow.com/questions/3834067/c-difference-between-and-in-parameter-passing) for some explanations on `*&` (as well as `**`). – ssell Mar 17 '17 at 21:01
  • @DIMITRIOS I updated your question, focusing on the gist of it, hope you don't mind. Don't forget to *accept* an answer BTW! – gsamaras Mar 17 '17 at 21:01

3 Answers3

2

Read it from right to left:

When you reach a *, replace it by pointer to.
When you reach a &, replace it by reference of.

So children will be: A reference of a pointer to an Object.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Rama
  • 3,222
  • 2
  • 11
  • 26
1

Second argument: Object* &children Object* says that children is a pointer to Object type. & prevent from receiving a copy of children from calling scope and lets us to work directly with argument variable, so when you change children :

children = new Object[num];

you change the argument variable in calling scope and after expand function returns, you have access to:

new Object[num]

via the argument variable that you passed to expand function.

Passing arguments by reference, is another way to receive information from function (It has other usages too).

Ali Asadpoor
  • 327
  • 1
  • 13
1

It's:

a reference of a pointer to an Object

The reason is that because it allocates dynamically memory, he probably wants this change to children to be reflected in the caller of the function (probably main()).

gsamaras
  • 71,951
  • 46
  • 188
  • 305