1

Newbie to C, C++ and QT.

I understand the usage of "&" for retreiving the address location pointed to by a pointer, but I am going through some C++ examples and I see the following:

int rowCount(const QModelIndex &parent = QModelIndex()) const ;
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);

Is the usage of the "&" in "&parent" and " & index" mean "pass-by-reference"?

If so then why is the "&" right before "parent" and there is a space in before "index"?

Also are these the same as writng it as "QModelIndex& parent"?

Thanks for your help. The syntax can be rather confusing.

Idov
  • 5,006
  • 17
  • 69
  • 106
nzxhvr70
  • 29
  • 1
  • 7
    This questions shows a serious lack of understanding the technology involved and is therefore beyond the scope of SO. Please refer to an appropriate beginners tutorial or other relevant documentation first. – Devolus Jan 10 '14 at 14:31
  • 3
    @Devolus: Newbie questions are not off-topic on StackOverflow. So long as the question is well-formed and otherwise on-topic, posters can ask the most basic questions. – John Dibling Jan 10 '14 at 14:40
  • @JohnDibling, I find this a bit confusing, because on meta there are always discussions pointing out that SO is not a beginners learning place. This was among other things the reason of "minimum understanding". – Devolus Jan 10 '14 at 14:42

4 Answers4

3

You're correct that in a variable/parameter declaration, the ampersand (&) means "reference". In the context of a function parameter, that equates to "pass-by-reference".

C++ doesn't care much about whitespace. It lets you put spaces before and/or after various symbols/operators, such as the ampersand. It can tell them apart from identifiers (such as type and variable names) because identifiers can only have alphanumerics and underscores in them. That means these are all directly equivalent:

QModelIndex& parent;
QModelIndex &parent;
QModelIndex & parent;
Peter Bloomfield
  • 5,578
  • 26
  • 37
1

Is the usage of the "&" in "&parent" and " & index" mean "pass-by-reference"?

Yes.

If so then why is the "&" right before "parent" and there is a space in before "index"?

Because some people put spaces in different places to others. It doesn't affect the meaning.

Also are these the same as writng it as "QModelIndex& parent"?

They're the same as const QModelIndex& parent. Spaces don't affect the meaning, but the const qualifier specifies that the function can't (legitimately) modify the object.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Spaces never matter in cases like these, C++ almost always ignores whitespace.

So yes, that would be the same: both would declare the argements to be references.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

Is the usage of the "&" in "&parent" and " & index" mean "pass-by-reference"?

yes.

If so then why is the "&" right before "parent" and there is a space in before "index"?

& index is smae as &index

Also are these the same as writng it as "QModelIndex& parent"?

Yes.

haccks
  • 104,019
  • 25
  • 176
  • 264